Debian Codegraph @

pkg/networkmanager/team.jsx

raw
1 /*
2 * Copyright (C) 2021 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 import React, { useState, useContext } from 'react';
7 import cockpit from 'cockpit';
8 import { FormGroup } from "@patternfly/react-core/dist/esm/components/Form/index.js";
9 import { FormSelect, FormSelectOption } from "@patternfly/react-core/dist/esm/components/FormSelect/index.js";
10 import { TextInput } from "@patternfly/react-core/dist/esm/components/TextInput/index.js";
11
12 import { MemberInterfaceChoices, NetworkModal, Name, dialogSave } from './dialogs-common.jsx';
13 import { ModelContext } from './model-context.jsx';
14 import { useDialogs } from "dialogs.jsx";
15
16 import { v4 as uuidv4 } from 'uuid';
17 import {
18 member_connection_for_interface,
19 member_interface_choices,
20 } from './interfaces.js';
21
22 const _ = cockpit.gettext;
23
24 export const team_runner_choices =
25 [
26 { choice: 'roundrobin', title: _("Round robin") },
27 { choice: 'activebackup', title: _("Active backup") },
28 { choice: 'loadbalance', title: _("Load balancing") },
29 { choice: 'broadcast', title: _("Broadcast") },
30 { choice: 'lacp', title: _("802.3ad LACP") },
31 ];
32
33 export const team_balancer_choices =
34 [
35 { choice: 'none', title: _("Passive") },
36 { choice: 'basic', title: _("Active") }
37 ];
38
39 export const team_watch_choices =
40 [
41 { choice: 'ethtool', title: _("Ethtool") },
42 { choice: 'arp-ping', title: _("ARP ping") },
43 { choice: 'nsna-ping', title: _("NSNA ping") }
44 ];
45
46 export const TeamDialog = ({ connection, dev, settings }) => {
47 const Dialogs = useDialogs();
48 const idPrefix = "network-team-settings";
49 const model = useContext(ModelContext);
50 const config = settings.team.config || {};
51 if (!config.runner)
52 config.runner = { };
53 if (!config.runner.name)
54 config.runner.name = "activebackup";
55 if (!config.link_watch)
56 config.link_watch = { };
57 if (!config.link_watch.name)
58 config.link_watch.name = "ethtool";
59 if (config.link_watch.interval === undefined)
60 config.link_watch.interval = 100;
61 if (config.link_watch.delay_up === undefined)
62 config.link_watch.delay_up = 0;
63 if (config.link_watch.delay_down === undefined)
64 config.link_watch.delay_down = 0;
65
66 const memberChoicesInit = {};
67
68 member_interface_choices(model, connection).forEach((iface) => {
69 memberChoicesInit[iface.Name] = !!member_connection_for_interface(connection, iface);
70 });
71
72 const [balancer, setBalancer] = useState(config.balancer);
73 const [dialogError, setDialogError] = useState(undefined);
74 const [iface, setIface] = useState(settings.connection.interface_name);
75 const [linkDownDelay, setLinkDownDelay] = useState(config.link_watch.delay_down);
76 const [linkWatch, setLinkWatch] = useState(config.link_watch.name);
77 const [linkUpDelay, setLinkUpDelay] = useState(config.link_watch.delay_up);
78 const [memberChoices, setMemberChoices] = useState(memberChoicesInit);
79 const [runner, setRunner] = useState(config.runner.name);
80 const [pingTarget, setPingTarget] = useState(config.link_watch.target_host);
81 const [pingInterval, setPingInterval] = useState(config.link_watch.interval);
82 const [primary, setPrimary] = useState(undefined);
83
84 const onSubmit = (ev) => {
85 const createSettingsObj = () => ({
86 ...settings,
87 connection: {
88 ...settings.connection,
89 id: iface,
90 interface_name: iface,
91 },
92 team: {
93 ...settings.team,
94 interface_name: iface,
95 config: {
96 ...settings.team.config,
97 link_watch: {
98 name: linkWatch,
99 ...(linkWatch == 'ethtool' && {
100 delay_up: linkUpDelay,
101 delay_down: linkDownDelay,
102 }),
103 ...(linkWatch != 'ethtool' && {
104 interval: pingInterval,
105 target_host: pingTarget,
106 }),
107 },
108 runner: {
109 ...config.runner,
110 name: runner,
111 ...((runner == "loadbalance" || runner == "lacp") && { tx_balancer: balancer == 'none' ? {} : balancer })
112 }
113 }
114 }
115 });
116
117 dialogSave({
118 model,
119 dev,
120 connection,
121 members: memberChoices,
122 membersInit: memberChoicesInit,
123 settings: createSettingsObj(),
124 setDialogError,
125 onClose: Dialogs.close,
126 });
127
128 // Prevent dialog from closing because of <form> onsubmit event
129 if (ev)
130 ev.preventDefault();
131
132 return false;
133 };
134
135 return (
136 <NetworkModal dialogError={dialogError}
137 idPrefix={idPrefix}
138 onSubmit={onSubmit}
139 title={!connection ? _("Add team") : _("Edit team settings")}
140 isCreateDialog={!connection}
141 >
142 <>
143 <Name idPrefix={idPrefix} iface={iface} setIface={setIface} />
144 <FormGroup label={_("Ports")} fieldId={idPrefix + "-interface-members-list"} hasNoPaddingTop>
145 <MemberInterfaceChoices idPrefix={idPrefix} memberChoices={memberChoices} setMemberChoices={setMemberChoices} model={model} group={connection} />
146 </FormGroup>
147 <FormGroup fieldId={idPrefix + "-runner-select"} label={_("Runner")}>
148 <FormSelect id={idPrefix + "-runner-select"} onChange={(_, val) => setRunner(val)}
149 value={runner}>
150 {team_runner_choices.map(choice => <FormSelectOption value={choice.choice} label={choice.title} key={choice.choice} />)}
151 </FormSelect>
152 </FormGroup>
153 {(runner == "loadbalance" || runner == "lacp") && <FormGroup fieldId={idPrefix + "-balancer-select"} label={_("Balancer")}>
154 <FormSelect id={idPrefix + "-balancer-select"} onChange={(_, val) => setBalancer(val)}
155 value={balancer}>
156 {team_balancer_choices.map(choice => <FormSelectOption value={choice.choice} label={choice.title} key={choice.choice} />)}
157 </FormSelect>
158 </FormGroup>}
159 {runner == "activebackup" && <FormGroup fieldId={idPrefix + "-primary-select"} label={_("Primary")}>
160 <FormSelect id={idPrefix + "-primary-select"} onChange={(_, val) => setPrimary(val)}
161 value={primary}>
162 <>
163 <FormSelectOption key='-' value={null} label='-' />
164 {Object.keys(memberChoices)
165 .filter(iface => memberChoices[iface])
166 .map(iface => <FormSelectOption key={iface} label={iface} value={iface} />)}
167 </>
168 </FormSelect>
169 </FormGroup>}
170 <FormGroup fieldId={idPrefix + "-link-watch-select"} label={_("Link watch")}>
171 <FormSelect id={idPrefix + "-link-watch-select"} onChange={(_, val) => setLinkWatch(val)}
172 value={linkWatch}>
173 {team_watch_choices.map(choice => <FormSelectOption value={choice.choice} label={choice.title} key={choice.choice} />)}
174 </FormSelect>
175 </FormGroup>
176 {linkWatch == 'ethtool' && <>
177 <FormGroup fieldId={idPrefix + "-link-up-delay-input"} label={_("Link up delay")}>
178 <TextInput id={idPrefix + "-link-up-delay-input"} className="network-number-field" value={linkUpDelay} onChange={(_event, value) => setLinkUpDelay(value)} />
179 </FormGroup>
180 <FormGroup fieldId={idPrefix + "-link-down-delay-input"} label={_("Link down delay")}>
181 <TextInput id={idPrefix + "-link-down-delay-input"} className="network-number-field" value={linkDownDelay} onChange={(_event, value) => setLinkDownDelay(value)} />
182 </FormGroup>
183 </>}
184 {linkWatch != 'ethtool' && <>
185 <FormGroup fieldId={idPrefix + "-ping-interval-input"} label={_("Ping interval")}>
186 <TextInput id={idPrefix + "-ping-interval-input"} className="network-number-field" value={pingInterval} onChange={(_event, value) => setPingInterval(value)} />
187 </FormGroup>
188 <FormGroup fieldId={idPrefix + "-ping-target-input"} label={_("Ping target")}>
189 <TextInput id={idPrefix + "-ping-target-input"} value={pingTarget} onChange={(_event, value) => setPingTarget(value)} />
190 </FormGroup>
191 </>}
192 </>
193 </NetworkModal>
194 );
195 };
196
197 export const getGhostSettings = ({ newIfaceName }) => {
198 return (
199 {
200 connection: {
201 id: newIfaceName,
202 autoconnect: true,
203 type: "team",
204 uuid: uuidv4(),
205 interface_name: newIfaceName,
206 },
207 team: {
208 config: {},
209 interface_name: newIfaceName
210 }
211 }
212 );
213 };