Debian Codegraph @

pkg/networkmanager/network-main.jsx

raw
1 /*
2 * Copyright (C) 2021 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 import cockpit from "cockpit";
7 import React from 'react';
8 import { useEvent } from "hooks";
9
10 import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js";
11 import { Label } from "@patternfly/react-core/dist/esm/components/Label/index.js";
12 import { Card, CardBody, CardHeader, CardTitle } from '@patternfly/react-core/dist/esm/components/Card/index.js';
13 import { Flex, FlexItem } from "@patternfly/react-core/dist/esm/layouts/Flex/index.js";
14 import { Gallery } from "@patternfly/react-core/dist/esm/layouts/Gallery/index.js";
15 import { Page, PageSection, } from "@patternfly/react-core/dist/esm/components/Page/index.js";
16 import { ConnectedIcon } from "@patternfly/react-icons";
17
18 import { FirewallSwitch } from "./firewall-switch.jsx";
19 import { ListingTable } from "cockpit-components-table.jsx";
20 import { NetworkAction } from "./dialogs-common.jsx";
21 import { LogsPanel } from "cockpit-components-logs-panel.jsx";
22 import { NetworkPlots } from "./plots";
23 import { in_anaconda_mode } from "utils";
24
25 import firewall from './firewall-client.js';
26 import {
27 device_state_text,
28 is_managed,
29 render_active_connection,
30 } from './interfaces.js';
31
32 const _ = cockpit.gettext;
33
34 export const NetworkPage = ({ privileged, operationInProgress, usage_monitor, plot_state, interfaces }) => {
35 useEvent(firewall, "changed");
36 useEvent(usage_monitor.grid, "notify");
37
38 const managed = [];
39 const unmanaged = [];
40 const plot_ifaces = [];
41 let hasDetails = false;
42
43 interfaces.forEach(iface => {
44 function hasGroup(iface) {
45 return ((iface.Device &&
46 iface.Device.ActiveConnection &&
47 iface.Device.ActiveConnection.Group &&
48 iface.Device.ActiveConnection.Group.Members.length > 0) ||
49 (iface.MainConnection &&
50 iface.MainConnection.Groups.length > 0));
51 }
52
53 // Skip loopback
54 if (iface.Name == "lo" || (iface.Device && iface.Device.DeviceType == 'loopback'))
55 return;
56
57 // Skip members
58 if (hasGroup(iface))
59 return;
60
61 const dev = iface.Device;
62 const show_traffic = (dev && (dev.State == 100 || dev.State == 10) && dev.Carrier === true);
63
64 plot_ifaces.push(iface.Name);
65 usage_monitor.add(iface.Name);
66
67 const activeConnection = render_active_connection(dev, false, true);
68 const row = {
69 columns: [
70 { title: (!dev || is_managed(dev)) ? <Button variant="link" isInline onClick={() => cockpit.location.go([iface.Name])}>{iface.Name}</Button> : iface.Name },
71 { title: activeConnection },
72 ],
73 props: {
74 key: iface.Name,
75 "data-interface": encodeURIComponent(iface.Name),
76 "data-sample-id": show_traffic ? encodeURIComponent(iface.Name) : null,
77 "data-row-id": iface.Name,
78 }
79 };
80
81 if (show_traffic) {
82 const samples = usage_monitor.samples[iface.Name];
83 row.columns.push({ title: samples ? cockpit.format_bits_per_sec(samples[1][0] * 8) : "" });
84 row.columns.push({ title: samples ? cockpit.format_bits_per_sec(samples[0][0] * 8) : "" });
85 } else {
86 row.columns.push({ title: device_state_text(dev), props: { colSpan: 2 } });
87 }
88
89 // Details column: show type-specific information
90 let detailsColumn = null;
91 if (dev?.DeviceType === '802-11-wireless') {
92 const networkCount = dev.visibleSsids.length;
93 if (networkCount > 0 || dev.ActiveAccessPoint?.Ssid) {
94 hasDetails = true;
95 detailsColumn = (
96 <Flex columnGap={{ default: 'columnGapSm' }}>
97 {networkCount > 0 && (
98 <FlexItem>
99 <Label status="info">
100 {cockpit.format(cockpit.ngettext("$0 network", "$0 networks", networkCount), networkCount)}
101 </Label>
102 </FlexItem>
103 )}
104 {dev.ActiveAccessPoint?.Ssid && (
105 <FlexItem>
106 <Label status="success" icon={<ConnectedIcon />}>{dev.ActiveAccessPoint?.Ssid}</Label>
107 </FlexItem>
108 )}
109 </Flex>
110 );
111 }
112 }
113 row.columns.push({ title: detailsColumn });
114
115 if (!dev || is_managed(dev)) {
116 managed.push(row);
117 } else {
118 unmanaged.push(row);
119 }
120 });
121
122 const rx_plot_data = {
123 direct: "network.interface.in.bytes",
124 internal: "network.interface.rx",
125 units: "bytes",
126 derive: "rate",
127 threshold: 200,
128 factor: 8
129 };
130
131 const tx_plot_data = {
132 direct: "network.interface.out.bytes",
133 internal: "network.interface.tx",
134 units: "bytes",
135 derive: "rate",
136 threshold: 200,
137 factor: 8
138 };
139
140 plot_state.plot_instances('rx', rx_plot_data, plot_ifaces);
141 plot_state.plot_instances('tx', tx_plot_data, plot_ifaces);
142
143 /* Start of properties for the LogsPanel component */
144 const match = [
145 "_SYSTEMD_UNIT=NetworkManager.service", "+",
146 "_SYSTEMD_UNIT=firewalld.service"
147 ];
148 const search_options = {
149 prio: "debug",
150 _SYSTEMD_UNIT: "NetworkManager.service,firewalld.service"
151 };
152 const url = "/system/logs/#/?prio=debug&_SYSTEMD_UNIT=NetworkManager.service,firewalld.service";
153 /* End of properties for the LogsPanel component */
154
155 const actions = privileged && (
156 <>
157 <NetworkAction buttonText={_("Add VPN")} type='wg' />
158 <NetworkAction buttonText={_("Add bond")} type='bond' />
159 <NetworkAction buttonText={_("Add team")} type='team' />
160 <NetworkAction buttonText={_("Add bridge")} type='bridge' />
161 <NetworkAction buttonText={_("Add VLAN")} type='vlan' />
162 </>
163 );
164
165 const anaconda = in_anaconda_mode();
166
167 return (
168 <Page data-test-wait={operationInProgress} id="networking" className={"pf-m-no-sidebar" + (anaconda ? " anaconda" : "")}>
169 <PageSection hasBodyWrapper={false} id="networking-graphs" className="networking-graphs">
170 <NetworkPlots plot_state={plot_state} />
171 </PageSection>
172 <PageSection hasBodyWrapper={false}>
173 <Gallery hasGutter>
174 {firewall.installed && <Card isPlain id="networking-firewall-summary">
175 <CardHeader actions={{
176 actions: <Button variant="secondary" id="networking-firewall-link"
177 component="a"
178 onClick={() => cockpit.jump("/network/firewall", cockpit.transport.host)}>
179 {_("Edit rules and zones")}
180 </Button>,
181 }}>
182 <Flex gap={{ default: 'gapMd' }} alignItems={{ default: 'alignItemsCenter' }}>
183 <CardTitle component="h2">{_("Firewall")}</CardTitle>
184 <FirewallSwitch firewall={firewall} />
185 </Flex>
186 </CardHeader>
187 <CardBody>
188 <Button variant="link"
189 component="a"
190 isInline
191 onClick={() => cockpit.jump("/network/firewall", cockpit.transport.host)}>
192 {cockpit.format(cockpit.ngettext("$0 active zone", "$0 active zones", firewall.activeZones.size), firewall.activeZones.size)}
193 </Button>
194 </CardBody>
195 </Card>}
196 <Card isPlain id="networking-interfaces">
197 <CardHeader actions={{ actions }}>
198 <CardTitle component="h2">{_("Interfaces")}</CardTitle>
199 </CardHeader>
200 <ListingTable aria-label={_("Managed interfaces")}
201 variant='compact'
202 columns={[
203 { title: _("Name"), header: true, props: { width: hasDetails ? 15 : 25 } },
204 { title: _("IP address"), props: { width: hasDetails ? 35 : 25 } },
205 { title: _("Sending"), props: { width: hasDetails ? 15 : 25 } },
206 { title: _("Receiving"), props: { width: hasDetails ? 15 : 25 } },
207 ...(hasDetails ? [{ title: _("Details"), props: { width: 20 } }] : []),
208 ]}
209 rows={managed} />
210 </Card>
211 {unmanaged.length > 0 &&
212 <Card isPlain id="networking-unmanaged-interfaces">
213 <CardHeader>
214 <CardTitle component="h2">{_("Unmanaged interfaces")}</CardTitle>
215 </CardHeader>
216 <ListingTable aria-label={_("Unmanaged interfaces")}
217 variant='compact'
218 columns={[
219 { title: _("Name"), header: true, props: { width: hasDetails ? 15 : 25 } },
220 { title: _("IP address"), props: { width: hasDetails ? 35 : 25 } },
221 { title: _("Sending"), props: { width: hasDetails ? 15 : 25 } },
222 { title: _("Receiving"), props: { width: hasDetails ? 15 : 25 } },
223 ...(hasDetails ? [{ title: _("Details"), props: { width: 20 } }] : []),
224 ]}
225 rows={unmanaged} />
226 </Card>}
227 <LogsPanel title={_("Network logs")} match={match}
228 max={10} search_options={search_options}
229 goto_url={url}
230 className="contains-list" />
231 </Gallery>
232 </PageSection>
233 </Page>
234 );
235 };