Debian Codegraph @

pkg/systemd/hwinfo.jsx

raw
1 /*
2 * Copyright (C) 2018 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 import 'cockpit-dark-theme'; // once per page
7 import '../lib/patternfly/patternfly-6-cockpit.scss';
8 import 'polyfills'; // once per application
9
10 import cockpit from "cockpit";
11 import React from "react";
12 import { createRoot } from 'react-dom/client';
13
14 import * as timeformat from 'timeformat';
15
16 import { Alert, AlertActionCloseButton } from "@patternfly/react-core/dist/esm/components/Alert/index.js";
17 import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js";
18 import { Card, CardBody, CardHeader, CardTitle } from "@patternfly/react-core/dist/esm/components/Card/index.js";
19 import { DataList, DataListAction, DataListCell, DataListItem, DataListItemCells, DataListItemRow } from "@patternfly/react-core/dist/esm/components/DataList/index.js";
20 import { DescriptionList, DescriptionListDescription, DescriptionListGroup, DescriptionListTerm } from "@patternfly/react-core/dist/esm/components/DescriptionList/index.js";
21 import { EmptyState } from "@patternfly/react-core/dist/esm/components/EmptyState/index.js";
22 import { Flex, FlexItem } from "@patternfly/react-core/dist/esm/layouts/Flex/index.js";
23 import { Gallery } from "@patternfly/react-core/dist/esm/layouts/Gallery/index.js";
24 import { Page, PageBreadcrumb, PageSection } from "@patternfly/react-core/dist/esm/components/Page/index.js";
25 import { Content, ContentVariants } from "@patternfly/react-core/dist/esm/components/Content/index.js";
26 import { Breadcrumb, BreadcrumbItem } from "@patternfly/react-core/dist/esm/components/Breadcrumb/index.js";
27 import {
28 Modal, ModalBody, ModalFooter, ModalHeader
29 } from '@patternfly/react-core/dist/esm/components/Modal/index.js';
30 import { Switch } from "@patternfly/react-core/dist/esm/components/Switch/index.js";
31 import { ExternalLinkAltIcon } from "@patternfly/react-icons";
32 import { SortByDirection } from "@patternfly/react-table";
33 import { ListingTable } from "cockpit-components-table.jsx";
34 import { WithDialogs, useDialogs } from "dialogs.jsx";
35
36 import kernelopt_sh from "kernelopt.sh";
37 import detect from "./hw-detect.js";
38
39 import { superuser } from "superuser";
40 import { PrivilegedButton } from "cockpit-components-privileged";
41 import { useInit } from "hooks";
42
43 import "./hwinfo.scss";
44
45 const _ = cockpit.gettext;
46
47 const SystemInfo = ({ info, onSecurityClick }) => {
48 if ((!info.name || !info.version) && info.alt_name && info.alt_version) {
49 info.name = info.alt_name;
50 info.version = info.alt_version;
51 }
52
53 const mitigations = (
54 <PrivilegedButton variant="link" buttonId="cpu_mitigations" tooltipId="tip-cpu-security"
55 excuse={ _("The user $0 is not permitted to change cpu security mitigations") }
56 onClick={ onSecurityClick }>
57 { _("Mitigations") }
58 </PrivilegedButton>
59 );
60
61 const bios_date = Date.parse(info.bios_date); // NaN for undefined, null, or invalid dates
62
63 return (
64 <Flex id="hwinfo-system-info-list" direction={{ default: 'column', sm: 'row' }}>
65 <FlexItem className="hwinfo-system-info-list-item" flex={{ default: 'flex_1' }}>
66 <DescriptionList className="pf-m-horizontal-on-md">
67 { info.type &&
68 <DescriptionListGroup>
69 <DescriptionListTerm>{ _("Type") }</DescriptionListTerm>
70 <DescriptionListDescription>{ info.type }</DescriptionListDescription>
71 </DescriptionListGroup> }
72 { info.name &&
73 <DescriptionListGroup>
74 <DescriptionListTerm>{ _("Name") }</DescriptionListTerm>
75 <DescriptionListDescription>{ info.name }</DescriptionListDescription>
76 </DescriptionListGroup> }
77 { info.version &&
78 <DescriptionListGroup>
79 <DescriptionListTerm>{ _("Version") }</DescriptionListTerm>
80 <DescriptionListDescription>{ info.version }</DescriptionListDescription>
81 </DescriptionListGroup> }
82 </DescriptionList>
83 </FlexItem>
84 <FlexItem className="hwinfo-system-info-list-item" flex={{ default: 'flex_1' }}>
85 <DescriptionList className="pf-m-horizontal-on-md">
86 { info.bios_vendor && <>
87 <DescriptionListGroup>
88 <DescriptionListTerm>{ _("BIOS") }</DescriptionListTerm>
89 <DescriptionListDescription>{ info.bios_vendor }</DescriptionListDescription>
90 </DescriptionListGroup>
91 <DescriptionListGroup>
92 <DescriptionListTerm>{ _("BIOS version") }</DescriptionListTerm>
93 <DescriptionListDescription>{ info.bios_version }</DescriptionListDescription>
94 </DescriptionListGroup>
95 <DescriptionListGroup>
96 <DescriptionListTerm>{ _("BIOS date") }</DescriptionListTerm>
97 <DescriptionListDescription>{ bios_date ? timeformat.date(bios_date) : info.bios_date }</DescriptionListDescription>
98 </DescriptionListGroup>
99 </> }
100 { info.nproc !== undefined && <>
101 <DescriptionListGroup>
102 <DescriptionListTerm>{ _("CPU") }</DescriptionListTerm>
103 <DescriptionListDescription>{ (info.nproc > 1) ? `${info.nproc}x ${info.cpu_model}` : info.cpu_model }</DescriptionListDescription>
104 </DescriptionListGroup>
105 { onSecurityClick !== undefined && <DescriptionListGroup>
106 <DescriptionListTerm>{ _("CPU security") }</DescriptionListTerm>
107 <DescriptionListDescription>{ mitigations }</DescriptionListDescription>
108 </DescriptionListGroup>}
109 </> }
110 </DescriptionList>
111 </FlexItem>
112 </Flex>
113 );
114 };
115
116 let cachedMitigations;
117
118 function availableMitigations() {
119 if (cachedMitigations !== undefined)
120 return Promise.resolve(cachedMitigations);
121 /* nosmt */
122 const promises = [cockpit.spawn(["lscpu"], { environ: ["LC_ALL=C.UTF-8"], }), cockpit.file("/proc/cmdline").read()];
123 return Promise.all(promises).then(values => {
124 let threads_per_core;
125 try {
126 threads_per_core = Number(values[0].split('\n')
127 .find(l => l.indexOf('Thread(s) per core:') !== -1)
128 .split(':')[1]);
129 } catch (e) {
130 console.warn(e);
131 return { available: false };
132 }
133 /* "nosmt" and "nosmt=force" are valid */
134 const nosmt_enabled = (values[1].indexOf("nosmt") !== -1 && values[1].indexOf("nosmt=") === -1) || values[1].indexOf("nosmt=force") !== -1;
135 /* available if threads>1 and the cmdline is valid */
136 const nosmt_available = threads_per_core > 1 && (values[1].indexOf("nosmt=") === -1 || values[1].indexOf("nosmt=force") !== -1);
137 const mitigations_match = values[1].match(/\bmitigations=(\S*)\b/);
138
139 cachedMitigations = {
140 available: nosmt_available,
141 nosmt_enabled,
142 mitigations_arg: mitigations_match ? mitigations_match[1] : undefined,
143 };
144 return cachedMitigations;
145 });
146 }
147
148 const CPUSecurityMitigationsDialog = () => {
149 const [nosmt, setNoSMT] = React.useState(undefined);
150 const [alert, setAlert] = React.useState(undefined);
151 const [rebooting, setRebooting] = React.useState(false);
152
153 useInit(() => {
154 availableMitigations().then(({ available, nosmt_enabled }) => {
155 setNoSMT(nosmt_enabled);
156 });
157 });
158
159 const saveAndReboot = () => {
160 let options = [];
161 if (nosmt) {
162 options = ['set', 'nosmt'];
163 } else {
164 // this may either be an argument of its own, or part of mitigations=
165 const ma = cachedMitigations.mitigations_arg;
166 if (ma && ma.indexOf("nosmt") >= 0) {
167 const new_args = ma.split(',').filter(opt => opt != 'nosmt');
168 options = ['set', 'mitigations=' + new_args.join(',')];
169 } else {
170 options = ['remove', 'nosmt'];
171 }
172 }
173
174 cockpit.script(kernelopt_sh, options, { superuser: "require", err: "message" })
175 .then(() => {
176 cockpit.spawn(["shutdown", "--reboot", "now"], { superuser: "require", err: "message" })
177 .catch(error => { setRebooting(false); setAlert(error.message) });
178 })
179 .catch(error => { setRebooting(false); setAlert(error.message) });
180 setRebooting(true);
181 };
182
183 const Dialogs = useDialogs();
184 const rows = [];
185 if (nosmt !== undefined) {
186 rows.push(
187 <DataListItem key="nosmt">
188 <DataListItemRow>
189 <DataListItemCells
190 dataListCells={[
191 <DataListCell key="primary content">
192 <span>
193 <div className='nosmt-heading'>{ _("Disable simultaneous multithreading") } (nosmt)</div>
194 <small className='nosmt-read-more-link'>
195 <a href="https://access.redhat.com/security/vulnerabilities/L1TF" target="_blank" rel="noopener noreferrer">
196 <ExternalLinkAltIcon /> { _("Read more...") }
197 </a>
198 </small>
199 </span>
200 </DataListCell>,
201 ]}
202 />
203 <DataListAction>
204 <div id="nosmt-switch">
205 <Switch isDisabled={rebooting}
206 onChange={(_event, value) => setNoSMT(value)}
207 isChecked={nosmt} />
208 </div>
209 </DataListAction>
210 </DataListItemRow>
211 </DataListItem>
212 );
213 }
214
215 const footer = (
216 <ModalFooter>
217 <Button variant='danger' isDisabled={rebooting || nosmt === undefined} onClick={saveAndReboot}>
218 { _("Save and reboot") }
219 </Button>
220 <Button variant='link' className='btn-cancel' isDisabled={rebooting} onClick={Dialogs.close}>
221 { _("Cancel") }
222 </Button>
223 </ModalFooter>
224 );
225
226 return (
227 <Modal isOpen id="cpu-mitigations-dialog"
228 position="top" variant="medium"
229 onClose={Dialogs.close}>
230 <ModalHeader title={ _("CPU security toggles") } />
231 <ModalBody>
232 <Content className='cpu-mitigations-dialog-info' component={ContentVariants.p}>
233 { _("Software-based workarounds help prevent CPU security issues. These mitigations have the side effect of reducing performance. Change these settings at your own risk.") }
234 </Content>
235 <DataList>
236 { rows }
237 </DataList>
238 { alert !== undefined &&
239 <Alert variant="danger"
240 actionClose={<AlertActionCloseButton onClose={() => setAlert(undefined)} />}
241 title={alert} />}
242 </ModalBody>
243 {footer}
244 </Modal>
245 );
246 };
247
248 const HardwareInfo = ({ info }) => {
249 const [mitigationsAvailable, setMitigationsAvailable] = React.useState(false);
250
251 useInit(() => {
252 availableMitigations().then(({ available }) => setMitigationsAvailable(available));
253 });
254
255 const Dialogs = useDialogs();
256 let pci = null;
257 let memory = null;
258
259 if (info.pci.length > 0) {
260 const sortedPci = info.pci.concat();
261
262 pci = (
263 <ListingTable aria-label={ _("PCI") }
264 sortBy={{ index: 0, direction: SortByDirection.asc }}
265 columns={ [
266 { title: _("Class"), sortable: true },
267 { title: _("Model"), sortable: true },
268 { title: _("Vendor"), sortable: true },
269 { title: _("Slot"), sortable: true }
270 ] }
271 rows={ sortedPci.map(dev => ({
272 props: { key: dev.slot },
273 columns: [dev.cls, dev.model, dev.vendor, dev.slot]
274 }))} />
275 );
276 }
277
278 if (info.memory.length > 0) {
279 memory = (
280 <ListingTable aria-label={ _("Memory") }
281 columns={ [_("ID"), _("Memory technology"), _("Type"), _("Size"), _("State"), _("Rank"), _("Speed")]}
282 rows={ info.memory.map(dimm => ({
283 props: { key: dimm.locator },
284 columns: [dimm.locator, dimm.technology, dimm.type, dimm.size, dimm.state, dimm.rank, dimm.speed]
285 })) } />
286 );
287 } else if (!superuser.allowed) {
288 memory = (<EmptyState>
289 {_("Viewing memory information requires administrative access.")}
290 </EmptyState>);
291 }
292
293 return (
294 <Page className='pf-m-no-sidebar'>
295 <PageBreadcrumb hasBodyWrapper={false} stickyOnBreakpoint={{ default: "top" }}>
296 <Breadcrumb>
297 <BreadcrumbItem onClick={ () => cockpit.jump("/system", cockpit.transport.host)} className="pf-v6-c-breadcrumb__link">{ _("Overview") }</BreadcrumbItem>
298 <BreadcrumbItem isActive>{ _("Hardware information") }</BreadcrumbItem>
299 </Breadcrumb>
300 </PageBreadcrumb>
301 <PageSection hasBodyWrapper={false}>
302 <Gallery hasGutter>
303 <Card isPlain>
304 <CardHeader>
305 <CardTitle component="h2">{_("System information")}</CardTitle>
306 </CardHeader>
307 <CardBody>
308 <SystemInfo info={info.system}
309 onSecurityClick={mitigationsAvailable
310 ? () => Dialogs.show(<CPUSecurityMitigationsDialog />)
311 : undefined } />
312 </CardBody>
313 </Card>
314 <Card isPlain id="pci-listing">
315 <CardHeader>
316 <CardTitle component="h2">{_("PCI")}</CardTitle>
317 </CardHeader>
318 <CardBody className="contains-list">
319 { pci }
320 </CardBody>
321 </Card>
322 <Card isPlain id="memory-listing">
323 <CardHeader>
324 <CardTitle component="h2">{_("Memory")}</CardTitle>
325 </CardHeader>
326 <CardBody className="contains-list">
327 { memory }
328 </CardBody>
329 </Card>
330 </Gallery>
331 </PageSection>
332 </Page>
333 );
334 };
335
336 document.addEventListener("DOMContentLoaded", () => {
337 document.title = cockpit.gettext(document.title);
338 detect().then(info => {
339 const root = createRoot(document.getElementById('hwinfo'));
340 root.render(<WithDialogs><HardwareInfo info={info} /></WithDialogs>);
341 });
342 });