From b071821feefeb12a80723ada62c40a792e68b807 Mon Sep 17 00:00:00 2001 From: Kyuhwi Choi Date: Wed, 1 Jun 2016 11:33:27 +0900 Subject: [PATCH] [ONOS-4480] Implement select group based load balancing - Implements select group handler for vxlan tunnel based network - Implements update bucket method for select type group Change-Id: Ic58f298f9be6d5c9c8036fb521c2ffb6d0bb62ee --- .../impl/ScalableGatewayManager.java | 18 +- .../impl/SelectGroupHandler.java | 188 ++++++++++++++++++ 2 files changed, 204 insertions(+), 2 deletions(-) create mode 100644 apps/scalablegateway/src/main/java/org/onosproject/scalablegateway/impl/SelectGroupHandler.java diff --git a/apps/scalablegateway/src/main/java/org/onosproject/scalablegateway/impl/ScalableGatewayManager.java b/apps/scalablegateway/src/main/java/org/onosproject/scalablegateway/impl/ScalableGatewayManager.java index f837546be4..44db6344d8 100644 --- a/apps/scalablegateway/src/main/java/org/onosproject/scalablegateway/impl/ScalableGatewayManager.java +++ b/apps/scalablegateway/src/main/java/org/onosproject/scalablegateway/impl/ScalableGatewayManager.java @@ -39,6 +39,10 @@ import org.onosproject.net.config.NetworkConfigRegistry; import org.onosproject.net.config.NetworkConfigService; import org.onosproject.net.config.basics.SubjectFactories; import org.onosproject.net.device.DeviceService; +import org.onosproject.net.driver.DriverService; +import org.onosproject.net.group.Group; +import org.onosproject.net.group.GroupDescription; +import org.onosproject.net.group.GroupService; import org.onosproject.scalablegateway.api.GatewayNode; import org.onosproject.scalablegateway.api.GatewayNodeConfig; import org.onosproject.scalablegateway.api.ScalableGatewayService; @@ -81,7 +85,14 @@ public class ScalableGatewayManager implements ScalableGatewayService { @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) protected DeviceService deviceService; + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) + protected DriverService driverService; + + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) + protected GroupService groupService; + private GatewayNodeConfig config; + private SelectGroupHandler selectGroupHandler; private final NetworkConfigListener configListener = new InternalConfigListener(); @@ -100,6 +111,7 @@ public class ScalableGatewayManager implements ScalableGatewayService { configRegistry.registerConfigFactory(configFactory); configService.addListener(configListener); + selectGroupHandler = new SelectGroupHandler(groupService, deviceService, driverService, appId); readConfiguration(); log.info("started"); @@ -141,8 +153,10 @@ public class ScalableGatewayManager implements ScalableGatewayService { @Override public GroupId getGroupIdForGatewayLoadBalance(DeviceId srcDeviceId) { - //TODO: Implement group generation method by handler - return null; + GroupDescription description = selectGroupHandler.createSelectGroupInVxlan(srcDeviceId, getGatewayNodes()); + groupService.addGroup(description); + Group group = groupService.getGroup(description.deviceId(), description.appCookie()); + return group != null ? group.id() : null; } @Override diff --git a/apps/scalablegateway/src/main/java/org/onosproject/scalablegateway/impl/SelectGroupHandler.java b/apps/scalablegateway/src/main/java/org/onosproject/scalablegateway/impl/SelectGroupHandler.java new file mode 100644 index 0000000000..717379b660 --- /dev/null +++ b/apps/scalablegateway/src/main/java/org/onosproject/scalablegateway/impl/SelectGroupHandler.java @@ -0,0 +1,188 @@ +/* + * Copyright 2016-present Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onosproject.scalablegateway.impl; + +import com.google.common.collect.Lists; +import org.onlab.packet.Ip4Address; +import org.onosproject.core.ApplicationId; +import org.onosproject.net.DeviceId; +import org.onosproject.net.Port; +import org.onosproject.net.PortNumber; +import org.onosproject.net.behaviour.ExtensionTreatmentResolver; +import org.onosproject.net.device.DeviceService; +import org.onosproject.net.driver.DefaultDriverData; +import org.onosproject.net.driver.DefaultDriverHandler; +import org.onosproject.net.driver.Driver; +import org.onosproject.net.driver.DriverHandler; +import org.onosproject.net.driver.DriverService; +import org.onosproject.net.flow.DefaultTrafficTreatment; +import org.onosproject.net.flow.TrafficTreatment; +import org.onosproject.net.flow.instructions.ExtensionPropertyException; +import org.onosproject.net.flow.instructions.ExtensionTreatment; +import org.onosproject.net.flow.instructions.ExtensionTreatmentType; +import org.onosproject.net.group.DefaultGroupDescription; +import org.onosproject.net.group.DefaultGroupKey; +import org.onosproject.net.group.Group; +import org.onosproject.net.group.GroupBucket; +import org.onosproject.net.group.GroupBuckets; +import org.onosproject.net.group.GroupDescription; +import org.onosproject.net.group.GroupKey; +import org.onosproject.net.group.GroupService; +import org.onosproject.scalablegateway.api.GatewayNode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + +import static org.onosproject.net.group.DefaultGroupBucket.createSelectGroupBucket; + +/** + * Handles group generation request from ScalableGateway. + */ +public class SelectGroupHandler { + + private final Logger log = LoggerFactory.getLogger(getClass()); + + private static final String TUNNEL_DESTINATION = "tunnelDst"; + private static final String PORTNAME_PREFIX_TUNNEL = "vxlan"; + private static final String PORTNAME = "portName"; + + private final GroupService groupService; + private final DeviceService deviceService; + private final DriverService driverService; + private final ApplicationId appId; + + /** + * Default constructor. + * + * @param targetGroupService group service + * @param targetDeviceService device service + * @param targetDriverService driver service + * @param appId application id for group service + */ + public SelectGroupHandler(GroupService targetGroupService, DeviceService targetDeviceService, + DriverService targetDriverService, ApplicationId appId) { + groupService = targetGroupService; + deviceService = targetDeviceService; + driverService = targetDriverService; + this.appId = appId; + } + + /** + * Creates select type group description according to given deviceId. + * + * @param srcDeviceId target device id for group description + * @param nodeList gateway node list for bucket action + * @return created select type group description + */ + public GroupDescription createSelectGroupInVxlan(DeviceId srcDeviceId, List nodeList) { + List bucketList = generateBucketsForSelectGroup(srcDeviceId, nodeList); + GroupKey key = generateGroupKey(srcDeviceId, nodeList); + return new DefaultGroupDescription(srcDeviceId, GroupDescription.Type.SELECT, + new GroupBuckets(bucketList), key, null, appId); + } + + private GroupKey generateGroupKey(DeviceId srcDeviceId, List nodeList) { + String cookie = srcDeviceId.toString(); + for (GatewayNode node : nodeList) { + cookie = cookie.concat(node.getGatewayDeviceId().toString()); + } + return new DefaultGroupKey(cookie.getBytes()); + + } + + /** + * Updates groupBuckets in select type group. + * + * @param deviceId target device id for group description + * @param oldAppCookie group key for target group + * @param nodeList updated gateway node list for bucket action + * @param nodeInsertion update type(add or remove) + * @return result of process + */ + public boolean updateBucketToSelectGroupInVxlan(DeviceId deviceId, GroupKey oldAppCookie, + List nodeList, boolean nodeInsertion) { + List bucketList = generateBucketsForSelectGroup(deviceId, nodeList); + + GroupKey newAppCookie = generateGroupKey(deviceId, nodeList); + if (nodeInsertion) { + groupService.addBucketsToGroup(deviceId, oldAppCookie, + new GroupBuckets(bucketList), newAppCookie, appId); + } else { + groupService.removeBucketsFromGroup(deviceId, oldAppCookie, + new GroupBuckets(bucketList), newAppCookie, appId); + } + Group group = groupService.getGroup(deviceId, newAppCookie); + return group != null ? true : false; + } + + private List generateBucketsForSelectGroup(DeviceId deviceId, List nodeList) { + List bucketList = Lists.newArrayList(); + nodeList.forEach(node -> { + TrafficTreatment tBuilder = DefaultTrafficTreatment.builder() + .extension(buildNiciraExtenstion(deviceId, node.getDataIpAddress()), deviceId) + .setOutput(getTunnelPort(deviceId)) + .build(); + bucketList.add(createSelectGroupBucket(tBuilder)); + }); + return bucketList; + } + + /** + * Builds Nicira extension for tagging remoteIp of vxlan. + * + * @param id device id of vxlan source device + * @param hostIp remote ip of vxlan destination device + * @return NiciraExtension Treatment + */ + private ExtensionTreatment buildNiciraExtenstion(DeviceId id, Ip4Address hostIp) { + Driver driver = driverService.getDriver(id); + DriverHandler driverHandler = new DefaultDriverHandler(new DefaultDriverData(driver, id)); + ExtensionTreatmentResolver resolver = driverHandler.behaviour(ExtensionTreatmentResolver.class); + + ExtensionTreatment extensionInstruction = + resolver.getExtensionInstruction( + ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_TUNNEL_DST.type()); + + try { + extensionInstruction.setPropertyValue(TUNNEL_DESTINATION, hostIp); + } catch (ExtensionPropertyException e) { + log.error("Error setting Nicira extension setting {}", e); + } + + return extensionInstruction; + } + + /** + * Returns port number of vxlan tunnel. + * + * @param deviceId target Device Id + * @return portNumber + */ + private PortNumber getTunnelPort(DeviceId deviceId) { + Port port = deviceService.getPorts(deviceId).stream() + .filter(p -> p.annotations().value(PORTNAME).equals(PORTNAME_PREFIX_TUNNEL)) + .findAny().orElse(null); + + if (port == null) { + log.error("No TunnelPort was created."); + return null; + } + return port.number(); + + } +}