Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion api/src/main/java/com/cloud/bgp/BGPService.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,23 @@
import com.cloud.network.vpc.Vpc;
import com.cloud.utils.Pair;
import org.apache.cloudstack.api.command.user.bgp.ListASNumbersCmd;
import org.apache.cloudstack.framework.config.ConfigKey;
import org.apache.cloudstack.framework.config.Configurable;
import org.apache.cloudstack.network.BgpPeer;

import java.util.List;

public interface BGPService {
public interface BGPService extends Configurable {

ConfigKey<Integer> MinHoldtime = new ConfigKey<>(ConfigKey.CATEGORY_NETWORK, Integer.class,
"routed.bgp.min.holdtime",
"3",
"The lowest BGP hold time, in seconds, that a virtual router accepts. "
+ "It applies to every BGP peer on the router. "
+ "A session that negotiates a lower hold time, including 0, is refused. "
+ "The value is 0, which turns the minimum off, or 3 to 65535.",
true,
ConfigKey.Scope.Account);

ASNumberRange createASNumberRange(long zoneId, long startASNumber, long endASNumber);
List<ASNumberRange> listASNumberRanges(Long zoneId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

public class SetBgpPeersCommand extends NetworkElementCommand {
BgpPeerTO[] bpgPeers;
Integer minHoldtime;

protected SetBgpPeersCommand() {
}
Expand All @@ -36,4 +37,12 @@ public SetBgpPeersCommand(List<BgpPeerTO> bpgPeers) {
public BgpPeerTO[] getBpgPeers() {
return bpgPeers;
}

public Integer getMinHoldtime() {
return minHoldtime;
}

public void setMinHoldtime(Integer minHoldtime) {
this.minHoldtime = minHoldtime;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class SetBgpPeersConfigItem extends AbstractConfigItemFacade {
@Override
public List<ConfigItem> generateConfig(final NetworkElementCommand cmd) {
final SetBgpPeersCommand command = (SetBgpPeersCommand) cmd;
return generateConfigItems(new BgpPeers(Arrays.asList(command.getBpgPeers())));
return generateConfigItems(new BgpPeers(Arrays.asList(command.getBpgPeers()), command.getMinHoldtime()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

public class BgpPeers extends ConfigBase {
private List<BgpPeerTO> peers;
private Integer minHoldtime;

public BgpPeers() {
super(ConfigBase.BGP_PEERS);
Expand All @@ -35,11 +36,24 @@ public BgpPeers(List<BgpPeerTO> bgpPeers) {
this.peers = bgpPeers;
}

public BgpPeers(List<BgpPeerTO> bgpPeers, Integer minHoldtime) {
this(bgpPeers);
this.minHoldtime = minHoldtime;
}

public List<BgpPeerTO> getPeers() {
return peers;
}

public void setPeers(List<BgpPeerTO> bgpPeers) {
this.peers = bgpPeers;
}

public Integer getMinHoldtime() {
return minHoldtime;
}

public void setMinHoldtime(Integer minHoldtime) {
this.minHoldtime = minHoldtime;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@
import com.cloud.agent.resource.virtualnetwork.VRScripts;

import org.apache.cloudstack.network.BgpPeerTO;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class SetBgpPeersConfigItemTest {
Expand All @@ -53,4 +56,22 @@ public void testSetBgpPeersConfigItem() {
Assert.assertTrue((((FileConfigItem) configItems.get(0)).getFileName().startsWith(VRScripts.BGP_PEERS_CONFIG)));
Assert.assertEquals(VRScripts.UPDATE_CONFIG, ((ScriptConfigItem) configItems.get(1)).getScript());
}

private JsonObject bgpPeersFile(int minHoldtime) {
List<BgpPeerTO> bgpPeerTOs = new ArrayList<>();
bgpPeerTOs.add(new BgpPeerTO(1L, "198.51.100.1", null, 65100L, null, 10L, 64600L, "192.0.2.0/24", null, new HashMap<>()));
SetBgpPeersCommand command = new SetBgpPeersCommand(bgpPeerTOs);
command.setMinHoldtime(minHoldtime);

List<ConfigItem> configItems = new SetBgpPeersConfigItem().generateConfig(command);
return JsonParser.parseString(((FileConfigItem) configItems.get(0)).getFileContents()).getAsJsonObject();
}

@Test
public void testSetBgpPeersConfigItemCarriesMinHoldtime() {
JsonObject file = bgpPeersFile(3);
Assert.assertEquals(3, file.get("min_holdtime").getAsInt());
Assert.assertEquals(1, file.getAsJsonArray("peers").size());
Assert.assertEquals(9, bgpPeersFile(9).get("min_holdtime").getAsInt());
}
}
11 changes: 11 additions & 0 deletions server/src/main/java/com/cloud/bgp/BGPServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import com.cloud.utils.exception.CloudRuntimeException;
import org.apache.cloudstack.api.command.user.bgp.ListASNumbersCmd;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.framework.config.ConfigKey;
import org.apache.cloudstack.network.BgpPeer;
import org.apache.cloudstack.network.BgpPeerVO;
import org.apache.cloudstack.network.RoutedIpv4Manager;
Expand Down Expand Up @@ -108,6 +109,16 @@ public class BGPServiceImpl implements BGPService {
public BGPServiceImpl() {
}

@Override
public String getConfigComponentName() {
return BGPService.class.getSimpleName();
}

@Override
public ConfigKey<?>[] getConfigKeys() {
return new ConfigKey<?>[] {MinHoldtime};
}

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_AS_RANGE_CREATE, eventDescription = "AS Range creation")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
import com.cloud.api.ApiDBUtils;
import com.cloud.api.query.dao.NetworkOfferingJoinDao;
import com.cloud.api.query.vo.NetworkOfferingJoinVO;
import com.cloud.bgp.BGPService;
import com.cloud.capacity.CapacityManager;
import com.cloud.capacity.dao.CapacityDao;
import com.cloud.cluster.ManagementServerHostVO;
Expand Down Expand Up @@ -1531,6 +1532,10 @@ protected String validateValueRange(String name, String value, Class<?> type, Co
return String.format("[%s] value should be between 0 and 255. 0 value will disable this feature.", name);
}
}
// BGP negotiates a hold time of 0 or 3 and up, so a minimum of 1 or 2 would act as 3.
if (BGPService.MinHoldtime.key().equalsIgnoreCase(name) && (val < 0 || val == 1 || val == 2 || val > 65535)) {
return String.format("[%s] value should be 0, or between 3 and 65535. 0 turns off the minimum.", name);
}
if (UnmanagedVMsManager.ThreadsOnMSToImportVMwareVMFiles.key().equalsIgnoreCase(name) ||
UnmanagedVMsManager.ThreadsOnKVMHostToImportVMwareVMFiles.key().equalsIgnoreCase(name)) {
if (val < -1 || val > 10) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import com.cloud.agent.api.to.PortForwardingRuleTO;
import com.cloud.agent.api.to.StaticNatRuleTO;
import com.cloud.agent.manager.Commands;
import com.cloud.bgp.BGPService;
import com.cloud.configuration.Config;
import com.cloud.configuration.ConfigurationManager;
import com.cloud.dc.ASNumberVO;
Expand Down Expand Up @@ -1630,6 +1631,7 @@ public void createBgpPeersCommands(final List<? extends BgpPeer> bgpPeers, final
}

final SetBgpPeersCommand cmd = new SetBgpPeersCommand(bgpPeerTOs);
cmd.setMinHoldtime(BGPService.MinHoldtime.valueIn(router.getAccountId()));
cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, _routerControlHelper.getRouterControlIp(router.getId()));
cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName());
final DataCenterVO dcVo = _dcDao.findById(router.getDataCenterId());
Expand Down
12 changes: 12 additions & 0 deletions server/src/test/java/com/cloud/bgp/BGPServiceImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.cloud.network.vpc.dao.VpcServiceMapDao;
import com.cloud.user.AccountVO;
import com.cloud.user.dao.AccountDao;
import org.apache.cloudstack.framework.config.ConfigKey;
import org.apache.cloudstack.network.BgpPeerVO;
import org.apache.cloudstack.network.RoutedIpv4Manager;
import org.apache.cloudstack.network.dao.BgpPeerDao;
Expand All @@ -43,6 +44,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.mockito.ArgumentMatchers.any;
Expand Down Expand Up @@ -173,4 +175,14 @@ public void testApplyBgpPeersForVpcWithBgpPeers() throws ResourceUnavailableExce

verify(vpcVirtualRouterElement).applyBgpPeers(eq(vpc), eq(null), any());
}

// The minimum hold time is a dynamic account setting registered by this service.
@Test
public void minHoldtimeIsDynamicAccountSettingDefaulting3() {
Assert.assertEquals("routed.bgp.min.holdtime", BGPService.MinHoldtime.key());
Assert.assertEquals("3", BGPService.MinHoldtime.defaultValue());
Assert.assertTrue(BGPService.MinHoldtime.isDynamic());
Assert.assertEquals(Collections.singletonList(ConfigKey.Scope.Account), BGPService.MinHoldtime.getScopes());
Assert.assertTrue(Arrays.asList(bGPServiceImplSpy.getConfigKeys()).contains(BGPService.MinHoldtime));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.cloud.configuration;

import com.cloud.alert.AlertManager;
import com.cloud.bgp.BGPService;
import com.cloud.capacity.dao.CapacityDao;
import com.cloud.dc.DataCenterVO;
import com.cloud.dc.VlanVO;
Expand Down Expand Up @@ -861,6 +862,20 @@ public void validateValueRangeTestReturnsNotNullWhenConfigKeyHasRangeAndValueIsI
Assert.assertNotNull(configurationManagerImplSpy.validateValueRange(NetworkModel.MACIdentifier.key(), "-1", Integer.class, null));
}

@Test
public void validateValueRangeTestAcceptsBgpMinHoldtimeWithinRange() {
for (String value : new String[] {"0", "3", "65535"}) {
Assert.assertNull(configurationManagerImplSpy.validateValueRange(BGPService.MinHoldtime.key(), value, Integer.class, null));
}
}

@Test
public void validateValueRangeTestRejectsBgpMinHoldtimeOutsideRange() {
for (String value : new String[] {"-1", "1", "2", "65536"}) {
Assert.assertNotNull(configurationManagerImplSpy.validateValueRange(BGPService.MinHoldtime.key(), value, Integer.class, null));
}
}

@Test
public void validateValueRangeTestValidatesValueWhenConfigHasRange() {
Config config = Config.SecStorageEncryptCopy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.cloud.agent.api.Command;
import com.cloud.agent.api.routing.SetBgpPeersCommand;
import com.cloud.bgp.BGPService;
import com.cloud.agent.api.routing.VmDataCommand;
import com.cloud.agent.manager.Commands;
import com.cloud.configuration.ConfigurationManager;
Expand Down Expand Up @@ -410,6 +411,75 @@ public void testCreateBgpPeersCommandsForVpc() {
Assert.assertEquals(4, ((SetBgpPeersCommand) cmd).getBpgPeers().length);
}

private SetBgpPeersCommand createVpcBgpPeersCommand() {
BgpPeerVO bgpPeer = Mockito.mock(BgpPeerVO.class);
Commands cmds = new Commands(Command.OnError.Stop);
VirtualRouter router = Mockito.mock(VirtualRouter.class);
NetworkVO network = Mockito.mock(NetworkVO.class);

long zoneId = 10L;
long vpcId = 11L;
when(router.getDataCenterId()).thenReturn(zoneId);
when(router.getVpcId()).thenReturn(vpcId);
when(router.getAccountId()).thenReturn(2L);
ASNumberVO asNumberVO = Mockito.mock(ASNumberVO.class);
when(asNumberDao.findByZoneAndVpcId(zoneId, vpcId)).thenReturn(asNumberVO);

long networkOfferingId = 12L;
NetworkOfferingVO offering = Mockito.mock(NetworkOfferingVO.class);
when(networkOfferingDao.findByIdIncludingRemoved(networkOfferingId)).thenReturn(offering);
when(offering.getRoutingMode()).thenReturn(NetworkOffering.RoutingMode.Dynamic);
NetworkVO network1 = Mockito.mock(NetworkVO.class);
when(network1.getNetworkOfferingId()).thenReturn(networkOfferingId);
when(networkDao.listByVpc(vpcId)).thenReturn(Collections.singletonList(network1));

DataCenterVO dc = Mockito.mock(DataCenterVO.class);
when(dcDao.findById(zoneId)).thenReturn(dc);
when(dc.getNetworkType()).thenReturn(DataCenter.NetworkType.Advanced);

commandSetupHelper.createBgpPeersCommands(Collections.singletonList(bgpPeer), router, cmds, network);

Assert.assertEquals(1, cmds.size());
return (SetBgpPeersCommand) cmds.toCommands()[0];
}

private ConfigDepotImpl initConfigDepot() {
ConfigDepotImpl configDepotMock = Mockito.mock(ConfigDepotImpl.class);
ConfigKey.init(configDepotMock);
return configDepotMock;
}

@Test
public void testCreateBgpPeersCommandsCarriesDefaultMinHoldtime() {
initConfigDepot();
Assert.assertEquals(Integer.valueOf(3), createVpcBgpPeersCommand().getMinHoldtime());
}

@Test
public void testCreateBgpPeersCommandsCarriesRaisedMinHoldtime() {
ConfigDepotImpl configDepotMock = initConfigDepot();
when(configDepotMock.getConfigStringValue(Mockito.eq(BGPService.MinHoldtime.key()), Mockito.any(), Mockito.any())).thenReturn("9");
Assert.assertEquals(Integer.valueOf(9), createVpcBgpPeersCommand().getMinHoldtime());
}

@Test
public void testCreateBgpPeersCommandsUsesAccountMinHoldtime() {
ConfigDepotImpl configDepotMock = initConfigDepot();
Mockito.lenient().when(configDepotMock.getConfigStringValue(Mockito.eq(BGPService.MinHoldtime.key()), Mockito.eq(ConfigKey.Scope.Global), Mockito.isNull()))
.thenReturn("5");
when(configDepotMock.getConfigStringValue(Mockito.eq(BGPService.MinHoldtime.key()), Mockito.eq(ConfigKey.Scope.Account), Mockito.eq(2L)))
.thenReturn("9");
Assert.assertEquals(Integer.valueOf(9), createVpcBgpPeersCommand().getMinHoldtime());
}

@Test
public void testCreateBgpPeersCommandsFallsBackToGlobalMinHoldtime() {
ConfigDepotImpl configDepotMock = initConfigDepot();
when(configDepotMock.getConfigStringValue(Mockito.eq(BGPService.MinHoldtime.key()), Mockito.eq(ConfigKey.Scope.Global), Mockito.isNull()))
.thenReturn("5");
Assert.assertEquals(Integer.valueOf(5), createVpcBgpPeersCommand().getMinHoldtime());
}

@Test
public void testDhcpLeaseTimeoutDefaultValue() {
// Test that the default value is 0 (infinite)
Expand Down
23 changes: 22 additions & 1 deletion systemvm/debian/opt/cloud/bin/cs/CsBgpPeers.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ def process(self):
else:
self.public_ip = self.cl.get_eth2_ip()

self.min_holdtime = self.dbag.get("min_holdtime")
self.peers = {}
for item in self.dbag:
if item == "id":
if item in ("id", "min_holdtime"):
continue
self._process_dbag_item(self.dbag[item])

Expand Down Expand Up @@ -104,10 +105,30 @@ def _access_list_set(self):

return

def _minimum_holdtime(self):
# 0 turns off the minimum and renders no line. BGP negotiates a hold time of 0 or 3 and up,
# so only 3-65535 is rendered. Any other value renders no line, and the rest of the file still applies.
if not self.min_holdtime:
return None
try:
value = int(self.min_holdtime)
except (TypeError, ValueError):
logging.error("Ignoring minimum hold time %s, which is not a whole number" % self.min_holdtime)
return None
if value == 0:
return None
if not 3 <= value <= 65535:
logging.error("Ignoring minimum hold time %s, which is outside 3-65535" % self.min_holdtime)
return None
return value

def _process_peers(self):
for as_number in self.peers.keys():
self.frr_conf.add("router bgp {}".format(as_number))
self.frr_conf.add(" bgp router-id {}".format(self.public_ip))
min_holdtime = self._minimum_holdtime()
if min_holdtime:
self.frr_conf.add(" bgp minimum-holdtime {}".format(min_holdtime))
if self.peers[as_number]['ip6_peers']:
self.frr_conf.add(" bgp default ipv6-unicast")
for ip4_peer in self.peers[as_number]['ip4_peers']:
Expand Down
3 changes: 3 additions & 0 deletions systemvm/debian/opt/cloud/bin/cs_bgppeers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ def merge(dbag, peers):
if 'peer_id' in peer:
key = "{}-{}".format(peer['peer_id'], peer['network_id'])
dbag[key] = peer
# The minimum hold time applies to every peer, so it sits beside them.
if peers.get('min_holdtime') is not None:
dbag['min_holdtime'] = peers['min_holdtime']
return dbag
Loading