| 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 { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js"; |
| 9 |
import { FormGroup } from "@patternfly/react-core/dist/esm/components/Form/index.js"; |
| 10 |
import { FormSelect, FormSelectOption } from "@patternfly/react-core/dist/esm/components/FormSelect/index.js"; |
| 11 |
import { Popover } from "@patternfly/react-core/dist/esm/components/Popover/index.js"; |
| 12 |
import { TextInput } from "@patternfly/react-core/dist/esm/components/TextInput/index.js"; |
| 13 |
import { ExternalLinkSquareAltIcon, HelpIcon } from '@patternfly/react-icons'; |
| 14 |
|
| 15 |
import { MacMenu, MemberInterfaceChoices, NetworkModal, Name, dialogSave } from './dialogs-common.jsx'; |
| 16 |
import { ModelContext } from './model-context.jsx'; |
| 17 |
import { useDialogs } from "dialogs.jsx"; |
| 18 |
|
| 19 |
import { v4 as uuidv4 } from 'uuid'; |
| 20 |
import { |
| 21 |
member_connection_for_interface, |
| 22 |
member_interface_choices, |
| 23 |
} from './interfaces.js'; |
| 24 |
|
| 25 |
const _ = cockpit.gettext; |
| 26 |
|
| 27 |
export const bond_mode_choices = |
| 28 |
[ |
| 29 |
{ choice: 'balance-rr', title: _("Round robin") }, |
| 30 |
{ choice: 'active-backup', title: _("Active backup") }, |
| 31 |
{ choice: 'balance-xor', title: _("XOR") }, |
| 32 |
{ choice: 'broadcast', title: _("Broadcast") }, |
| 33 |
{ choice: '802.3ad', title: _("802.3ad") }, |
| 34 |
{ choice: 'balance-tlb', title: _("Adaptive transmit load balancing") }, |
| 35 |
{ choice: 'balance-alb', title: _("Adaptive load balancing") } |
| 36 |
]; |
| 37 |
|
| 38 |
const bond_monitoring_choices = |
| 39 |
[ |
| 40 |
{ choice: 'mii', title: _("MII (recommended)") }, |
| 41 |
{ choice: 'arp', title: _("ARP") } |
| 42 |
]; |
| 43 |
|
| 44 |
const modes_with_primary = [ |
| 45 |
'active-backup', |
| 46 |
'balance-tlb', |
| 47 |
'balance-alb' |
| 48 |
]; |
| 49 |
|
| 50 |
export const BondDialog = ({ connection, dev, settings }) => { |
| 51 |
const Dialogs = useDialogs(); |
| 52 |
const idPrefix = "network-bond-settings"; |
| 53 |
const model = useContext(ModelContext); |
| 54 |
const options = settings.bond.options; |
| 55 |
|
| 56 |
const memberChoicesInit = {}; |
| 57 |
|
| 58 |
member_interface_choices(model, connection).forEach((iface) => { |
| 59 |
memberChoicesInit[iface.Name] = !!member_connection_for_interface(connection, iface); |
| 60 |
}); |
| 61 |
|
| 62 |
const [dialogError, setDialogError] = useState(undefined); |
| 63 |
const [iface, setIface] = useState(settings.connection.interface_name); |
| 64 |
const [linkDownDelay, setLinkDownDelay] = useState(options.downdelay || "0"); |
| 65 |
const [linkMonitoring, setLinkMonitoring] = useState(options.arp_interval ? "arp" : "mii"); |
| 66 |
const [linkMonitoringInterval, setLinkMonitoringInterval] = useState(options.miimon || options.arp_interval || "100"); |
| 67 |
const [linkUpDelay, setLinkUpDelay] = useState(options.updelay || "0"); |
| 68 |
const [mac, setMAC] = useState((settings.ethernet && settings.ethernet.assigned_mac_address) || ""); |
| 69 |
const [memberChoices, setMemberChoices] = useState(memberChoicesInit); |
| 70 |
const [mode, setMode] = useState(options.mode); |
| 71 |
const [monitoringTargets, setMonitoringTargets] = useState(options.arp_ip_target); |
| 72 |
const [primary, setPrimary] = useState(options.primary); |
| 73 |
|
| 74 |
const onSubmit = (ev) => { |
| 75 |
const options = settings.bond.options; |
| 76 |
delete options.primary; |
| 77 |
|
| 78 |
const createSettingsObj = () => ({ |
| 79 |
...settings, |
| 80 |
connection: { |
| 81 |
...settings.connection, |
| 82 |
id: iface, |
| 83 |
interface_name: iface, |
| 84 |
}, |
| 85 |
ethernet: { |
| 86 |
assigned_mac_address: mac |
| 87 |
}, |
| 88 |
bond: { |
| 89 |
...settings.bond, |
| 90 |
interface_name: iface, |
| 91 |
options: { |
| 92 |
...options, |
| 93 |
mode, |
| 94 |
...(linkMonitoring == 'mii' && { |
| 95 |
miimon: linkMonitoringInterval, |
| 96 |
updelay: linkUpDelay, |
| 97 |
downdelay: linkDownDelay, |
| 98 |
}), |
| 99 |
...(linkMonitoring === 'arp' && { |
| 100 |
arp_interval: linkMonitoringInterval, |
| 101 |
arp_ip_target: monitoringTargets, |
| 102 |
}), |
| 103 |
...(modes_with_primary.includes(mode) && primary && { primary }) |
| 104 |
} |
| 105 |
} |
| 106 |
}); |
| 107 |
|
| 108 |
dialogSave({ |
| 109 |
model, |
| 110 |
dev, |
| 111 |
connection, |
| 112 |
members: memberChoices, |
| 113 |
membersInit: memberChoicesInit, |
| 114 |
settings: createSettingsObj(), |
| 115 |
setDialogError, |
| 116 |
onClose: Dialogs.close, |
| 117 |
}); |
| 118 |
|
| 119 |
// Prevent dialog from closing because of <form> onsubmit event |
| 120 |
if (ev) |
| 121 |
ev.preventDefault(); |
| 122 |
|
| 123 |
return false; |
| 124 |
}; |
| 125 |
|
| 126 |
return ( |
| 127 |
<NetworkModal dialogError={dialogError} |
| 128 |
idPrefix={idPrefix} |
| 129 |
onSubmit={onSubmit} |
| 130 |
title={!connection ? _("Add bond") : _("Edit bond settings")} |
| 131 |
help={ |
| 132 |
<Popover |
| 133 |
headerContent={_("Network bond")} |
| 134 |
id="bond-help" |
| 135 |
bodyContent={ |
| 136 |
<div> |
| 137 |
{_("A network bond combines multiple network interfaces into one logical interface with higher throughput or redundancy.")} |
| 138 |
</div> |
| 139 |
} |
| 140 |
footerContent={ |
| 141 |
<Button component='a' |
| 142 |
rel="noopener noreferrer" target="_blank" |
| 143 |
variant='link' |
| 144 |
isInline |
| 145 |
icon={<ExternalLinkSquareAltIcon />} iconPosition="right" |
| 146 |
href="https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/9/html/configuring_and_managing_networking/configuring-network-bonding_configuring-and-managing-networking#proc_configuring-a-network-bond-by-using-the-rhel-web-console_configuring-network-bonding"> |
| 147 |
{_("Learn more")} |
| 148 |
</Button> |
| 149 |
} |
| 150 |
> |
| 151 |
<Button icon={<HelpIcon />} id="bond-help-popup-button" variant="plain" aria-label="Help" /> |
| 152 |
</Popover> |
| 153 |
} |
| 154 |
isCreateDialog={!connection} |
| 155 |
> |
| 156 |
<> |
| 157 |
<Name idPrefix={idPrefix} iface={iface} setIface={setIface} /> |
| 158 |
<FormGroup label={_("Interfaces")} fieldId={idPrefix + "-interface-members-list"} hasNoPaddingTop> |
| 159 |
<MemberInterfaceChoices idPrefix={idPrefix} memberChoices={memberChoices} setMemberChoices={setMemberChoices} model={model} group={connection} /> |
| 160 |
</FormGroup> |
| 161 |
<FormGroup fieldId={idPrefix + "-mac-input"} label={_("MAC")}> |
| 162 |
<MacMenu idPrefix={idPrefix} model={model} mac={mac} setMAC={setMAC} /> |
| 163 |
</FormGroup> |
| 164 |
<FormGroup fieldId={idPrefix + "-mode-select"} label={_("Mode")}> |
| 165 |
<FormSelect id={idPrefix + "-mode-select"} onChange={(_, val) => setMode(val)} |
| 166 |
value={mode}> |
| 167 |
{bond_mode_choices.map(choice => <FormSelectOption value={choice.choice} label={choice.title} key={choice.choice} />)} |
| 168 |
</FormSelect> |
| 169 |
</FormGroup> |
| 170 |
{modes_with_primary.includes(mode) && <FormGroup fieldId={idPrefix + "-primary-select"} label={_("Primary")}> |
| 171 |
<FormSelect id={idPrefix + "-primary-select"} onChange={(_, val) => setPrimary(val)} |
| 172 |
value={primary}> |
| 173 |
<> |
| 174 |
<FormSelectOption key='-' value='' label='-' /> |
| 175 |
{Object.keys(memberChoices) |
| 176 |
.filter(iface => memberChoices[iface]) |
| 177 |
.map(iface => <FormSelectOption key={iface} label={iface} value={iface} />)} |
| 178 |
</> |
| 179 |
</FormSelect> |
| 180 |
</FormGroup>} |
| 181 |
<FormGroup fieldId={idPrefix + "-link-monitoring-select"} label={_("Link monitoring")}> |
| 182 |
<FormSelect id={idPrefix + "-link-monitoring-select"} onChange={(_, val) => setLinkMonitoring(val)} |
| 183 |
value={linkMonitoring}> |
| 184 |
{bond_monitoring_choices.map(choice => <FormSelectOption value={choice.choice} label={choice.title} key={choice.choice} />)} |
| 185 |
</FormSelect> |
| 186 |
</FormGroup> |
| 187 |
<FormGroup fieldId={idPrefix + "-link-monitoring-interval-input"} label={_("Monitoring interval")}> |
| 188 |
<TextInput id={idPrefix + "-link-monitoring-interval-input"} className="network-number-field" value={linkMonitoringInterval} onChange={(_event, value) => setLinkMonitoringInterval(value)} /> |
| 189 |
</FormGroup> |
| 190 |
{linkMonitoring == 'mii' && <> |
| 191 |
<FormGroup fieldId={idPrefix + "-link-up-delay-input"} label={_("Link up delay")}> |
| 192 |
<TextInput id={idPrefix + "-link-up-delay-input"} className="network-number-field" value={linkUpDelay} onChange={(_event, value) => setLinkUpDelay(value)} /> |
| 193 |
</FormGroup> |
| 194 |
<FormGroup fieldId={idPrefix + "-link-down-delay-input"} label={_("Link down delay")}> |
| 195 |
<TextInput id={idPrefix + "-link-down-delay-input"} className="network-number-field" value={linkDownDelay} onChange={(_event, value) => setLinkDownDelay(value)} /> |
| 196 |
</FormGroup> |
| 197 |
</>} |
| 198 |
{linkMonitoring == 'arp' && <FormGroup fieldId={idPrefix + "-monitoring-targets-input"} label={_("Monitoring targets")}> |
| 199 |
<TextInput id={idPrefix + "-monitoring-targets-input"} value={monitoringTargets} onChange={(_event, value) => setMonitoringTargets(value)} /> |
| 200 |
</FormGroup>} |
| 201 |
</> |
| 202 |
</NetworkModal> |
| 203 |
); |
| 204 |
}; |
| 205 |
|
| 206 |
export const getGhostSettings = ({ newIfaceName }) => { |
| 207 |
return ( |
| 208 |
{ |
| 209 |
connection: { |
| 210 |
id: newIfaceName, |
| 211 |
autoconnect: true, |
| 212 |
type: "bond", |
| 213 |
uuid: uuidv4(), |
| 214 |
interface_name: newIfaceName, |
| 215 |
}, |
| 216 |
bond: { |
| 217 |
options: { |
| 218 |
mode: "active-backup" |
| 219 |
}, |
| 220 |
interface_name: newIfaceName |
| 221 |
} |
| 222 |
} |
| 223 |
); |
| 224 |
}; |