Jonathan Hart

Implemented control plane redirect for control traffic.

Modified SoftRouterPipeline to support new flow objectives.

Change-Id: Ia93bc927832444ba1f7cf20b276e4866789c9d30
......@@ -17,6 +17,7 @@ package org.onosproject.routing;
import org.onlab.packet.IpAddress;
import org.onosproject.routing.config.BgpConfig;
import org.onosproject.routing.config.RouterConfig;
import java.util.Collection;
......@@ -28,6 +29,7 @@ public interface RoutingService {
String ROUTER_APP_ID = "org.onosproject.router";
Class<BgpConfig> CONFIG_CLASS = BgpConfig.class;
Class<RouterConfig> ROUTER_CONFIG_CLASS = RouterConfig.class;
/**
* Starts the routing service.
......
......@@ -42,8 +42,6 @@ public class BgpConfig extends Config<ApplicationId> {
public static final String NAME = "name";
public static final String PEERS = "peers";
// TODO add methods for updating config
/**
* Gets the set of configured BGP speakers.
*
......
/*
* Copyright 2016 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.routing.config;
import org.onosproject.core.ApplicationId;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.config.Config;
/**
* Routing configuration.
*/
public class RouterConfig extends Config<ApplicationId> {
public static final String CP_CONNECT_POINT = "controlPlaneConnectPoint";
/**
* Returns the routing control plane connect point.
*
* @return control plane connect point
*/
public ConnectPoint getControlPlaneConnectPoint() {
return ConnectPoint.deviceConnectPoint(object.path(CP_CONNECT_POINT).asText());
}
}
......@@ -41,6 +41,7 @@ import org.onosproject.net.config.basics.SubjectFactories;
import org.onosproject.routing.config.BgpConfig;
import org.onosproject.routing.config.BgpPeer;
import org.onosproject.routing.config.BgpSpeaker;
import org.onosproject.routing.config.RouterConfig;
import org.onosproject.routing.config.Interface;
import org.onosproject.routing.config.LocalIpPrefixEntry;
import org.onosproject.routing.config.RoutingConfigurationService;
......@@ -100,24 +101,36 @@ public class RoutingConfigurationImpl implements RoutingConfigurationService {
private MacAddress virtualGatewayMacAddress;
private ConfigFactory configFactory =
new ConfigFactory(SubjectFactories.APP_SUBJECT_FACTORY, BgpConfig.class, "bgp") {
private ConfigFactory<ApplicationId, BgpConfig> bgpConfigFactory =
new ConfigFactory<ApplicationId, BgpConfig>(
SubjectFactories.APP_SUBJECT_FACTORY, BgpConfig.class, "bgp") {
@Override
public BgpConfig createConfig() {
return new BgpConfig();
}
};
private ConfigFactory<ApplicationId, RouterConfig> routerConfigFactory =
new ConfigFactory<ApplicationId, RouterConfig>(
SubjectFactories.APP_SUBJECT_FACTORY, RouterConfig.class, "router") {
@Override
public RouterConfig createConfig() {
return new RouterConfig();
}
};
@Activate
public void activate() {
registry.registerConfigFactory(configFactory);
registry.registerConfigFactory(bgpConfigFactory);
registry.registerConfigFactory(routerConfigFactory);
readConfiguration();
log.info("Routing configuration service started");
}
@Deactivate
public void deactivate() {
registry.unregisterConfigFactory(configFactory);
registry.unregisterConfigFactory(bgpConfigFactory);
registry.registerConfigFactory(routerConfigFactory);
log.info("Routing configuration service stopped");
}
......
/*
* Copyright 2016 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.routing.impl;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.onlab.packet.EthType;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService;
import org.onosproject.incubator.net.intf.Interface;
import org.onosproject.incubator.net.intf.InterfaceService;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.DeviceId;
import org.onosproject.net.PortNumber;
import org.onosproject.net.config.NetworkConfigEvent;
import org.onosproject.net.config.NetworkConfigListener;
import org.onosproject.net.config.NetworkConfigService;
import org.onosproject.net.device.DeviceEvent;
import org.onosproject.net.device.DeviceListener;
import org.onosproject.net.device.DeviceService;
import org.onosproject.net.flow.DefaultTrafficSelector;
import org.onosproject.net.flow.DefaultTrafficTreatment;
import org.onosproject.net.flow.TrafficSelector;
import org.onosproject.net.flow.TrafficTreatment;
import org.onosproject.net.flowobjective.DefaultForwardingObjective;
import org.onosproject.net.flowobjective.FlowObjectiveService;
import org.onosproject.net.flowobjective.ForwardingObjective;
import org.onosproject.net.host.InterfaceIpAddress;
import org.onosproject.routing.RoutingService;
import org.onosproject.routing.config.RouterConfig;
import org.slf4j.Logger;
import static org.slf4j.LoggerFactory.getLogger;
/**
* Manages connectivity between peers redirecting control traffic to a routing
* control plane available on the dataplane.
*/
@Component(immediate = true, enabled = false)
public class ControlPlaneRedirectManager {
private final Logger log = getLogger(getClass());
private static final int PRIORITY = 40001;
private static final String APP_NAME = "org.onosproject.cpredirect";
private ApplicationId appId;
private ConnectPoint controlPlaneConnectPoint;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected CoreService coreService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected DeviceService deviceService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected InterfaceService interfaceService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected FlowObjectiveService flowObjectiveService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected NetworkConfigService networkConfigService;
private final InternalDeviceListener deviceListener = new InternalDeviceListener();
private final InternalNetworkConfigListener networkConfigListener =
new InternalNetworkConfigListener();
@Activate
public void activate() {
this.appId = coreService.registerApplication(APP_NAME);
deviceService.addListener(deviceListener);
networkConfigService.addListener(networkConfigListener);
updateConfig();
}
@Deactivate
public void deactivate() {
deviceService.removeListener(deviceListener);
networkConfigService.removeListener(networkConfigListener);
}
private void updateConfig() {
ApplicationId routingAppId =
coreService.registerApplication(RoutingService.ROUTER_APP_ID);
RouterConfig config = networkConfigService.getConfig(
routingAppId, RoutingService.ROUTER_CONFIG_CLASS);
if (config == null) {
log.info("Router config not available");
return;
}
if (!config.getControlPlaneConnectPoint().equals(controlPlaneConnectPoint)) {
this.controlPlaneConnectPoint = config.getControlPlaneConnectPoint();
}
if (controlPlaneConnectPoint != null &&
deviceService.isAvailable(controlPlaneConnectPoint.deviceId())) {
notifySwitchAvailable();
}
}
private void notifySwitchAvailable() {
DeviceId deviceId = controlPlaneConnectPoint.deviceId();
interfaceService.getInterfaces().stream()
.filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
.forEach(this::addInterfaceForwarding);
log.info("Sent interface objectives to {}", controlPlaneConnectPoint.deviceId());
}
private void addInterfaceForwarding(Interface intf) {
log.debug("Adding interface objectives for {}", intf);
DeviceId deviceId = controlPlaneConnectPoint.deviceId();
PortNumber controlPlanePort = controlPlaneConnectPoint.port();
for (InterfaceIpAddress ip : intf.ipAddresses()) {
// IPv4 to router
TrafficSelector toSelector = DefaultTrafficSelector.builder()
.matchInPort(intf.connectPoint().port())
.matchEthDst(intf.mac())
.matchEthType(EthType.EtherType.IPV4.ethType().toShort())
.matchVlanId(intf.vlan())
.matchIPDst(ip.ipAddress().toIpPrefix())
.build();
TrafficTreatment toTreatment = DefaultTrafficTreatment.builder()
.setOutput(controlPlanePort)
.build();
flowObjectiveService.forward(deviceId,
buildForwardingObjective(toSelector, toTreatment));
// IPv4 from router
TrafficSelector fromSelector = DefaultTrafficSelector.builder()
.matchInPort(controlPlanePort)
.matchEthSrc(intf.mac())
.matchVlanId(intf.vlan())
.matchEthType(EthType.EtherType.IPV4.ethType().toShort())
.matchIPSrc(ip.ipAddress().toIpPrefix())
.build();
TrafficTreatment intfTreatment = DefaultTrafficTreatment.builder()
.setOutput(intf.connectPoint().port())
.build();
flowObjectiveService.forward(deviceId,
buildForwardingObjective(fromSelector, intfTreatment));
// ARP to router
toSelector = DefaultTrafficSelector.builder()
.matchInPort(intf.connectPoint().port())
.matchEthType(EthType.EtherType.ARP.ethType().toShort())
.matchVlanId(intf.vlan())
.build();
toTreatment = DefaultTrafficTreatment.builder()
.setOutput(controlPlanePort)
.punt()
.build();
flowObjectiveService.forward(deviceId,
buildForwardingObjective(toSelector, toTreatment));
// ARP from router
fromSelector = DefaultTrafficSelector.builder()
.matchInPort(controlPlanePort)
.matchEthSrc(intf.mac())
.matchVlanId(intf.vlan())
.matchEthType(EthType.EtherType.ARP.ethType().toShort())
.build();
intfTreatment = DefaultTrafficTreatment.builder()
.setOutput(intf.connectPoint().port())
.punt()
.build();
flowObjectiveService.forward(deviceId,
buildForwardingObjective(fromSelector, intfTreatment));
}
}
private ForwardingObjective buildForwardingObjective(TrafficSelector selector,
TrafficTreatment treatment) {
return DefaultForwardingObjective.builder()
.withSelector(selector)
.withTreatment(treatment)
.fromApp(appId)
.withPriority(PRIORITY)
.withFlag(ForwardingObjective.Flag.VERSATILE)
.add();
}
private class InternalDeviceListener implements DeviceListener {
@Override
public void event(DeviceEvent event) {
if (controlPlaneConnectPoint != null &&
event.subject().id().equals(controlPlaneConnectPoint.deviceId())) {
switch (event.type()) {
case DEVICE_ADDED:
case DEVICE_AVAILABILITY_CHANGED:
if (deviceService.isAvailable(event.subject().id())) {
log.info("Device connected {}", event.subject().id());
notifySwitchAvailable();
}
break;
case DEVICE_UPDATED:
case DEVICE_REMOVED:
case DEVICE_SUSPENDED:
case PORT_ADDED:
case PORT_UPDATED:
case PORT_REMOVED:
default:
break;
}
}
}
}
private class InternalNetworkConfigListener implements NetworkConfigListener {
@Override
public void event(NetworkConfigEvent event) {
if (event.subject().equals(RoutingService.ROUTER_CONFIG_CLASS)) {
switch (event.type()) {
case CONFIG_ADDED:
case CONFIG_UPDATED:
updateConfig();
break;
case CONFIG_REGISTERED:
case CONFIG_UNREGISTERED:
case CONFIG_REMOVED:
default:
break;
}
}
}
}
}
......@@ -350,10 +350,9 @@ public class SingleSwitchFibInstaller {
fob.withKey(Criteria.matchInPort(intf.connectPoint().port()))
.addCondition(Criteria.matchEthDst(intf.mac()))
.addCondition(Criteria.matchVlanId(intf.vlan()));
intf.ipAddresses().stream()
.forEach(ipaddr -> fob.addCondition(
Criteria.matchIPDst(
IpPrefix.valueOf(ipaddr.ipAddress(), 32))));
fob.withPriority(PRIORITY_OFFSET);
fob.permit().fromApp(appId);
flowObjectiveService.filter(
deviceId,
......
......@@ -51,6 +51,7 @@ public class Vrouter {
.add("org.onosproject.routing.fpm.FpmManager")
.add("org.onosproject.routing.impl.Router")
.add("org.onosproject.routing.impl.SingleSwitchFibInstaller")
.add("org.onosproject.routing.impl.ControlPlaneRedirectManager")
.build();
@Activate
......
......@@ -17,13 +17,11 @@ package org.onosproject.driver.pipeline;
import org.onlab.osgi.ServiceDirectory;
import org.onlab.packet.Ethernet;
import org.onlab.packet.MacAddress;
import org.onlab.packet.VlanId;
import org.onlab.util.KryoNamespace;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService;
import org.onosproject.net.DeviceId;
import org.onosproject.net.PortNumber;
import org.onosproject.net.behaviour.NextGroup;
import org.onosproject.net.behaviour.Pipeliner;
import org.onosproject.net.behaviour.PipelinerContext;
......@@ -56,7 +54,6 @@ import org.slf4j.Logger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.ConcurrentHashMap;
import static org.slf4j.LoggerFactory.getLogger;
......@@ -81,8 +78,6 @@ public class SoftRouterPipeline extends AbstractHandlerBehaviour implements Pipe
protected DeviceId deviceId;
protected ApplicationId appId;
private ApplicationId driverId;
private Collection<Filter> filters;
private Collection<ForwardingObjective> pendingVersatiles;
private KryoNamespace appKryo = new KryoNamespace.Builder()
.register(DummyGroup.class)
......@@ -101,9 +96,7 @@ public class SoftRouterPipeline extends AbstractHandlerBehaviour implements Pipe
flowObjectiveStore = context.store();
driverId = coreService.registerApplication(
"org.onosproject.driver.SoftRouterPipeline");
filters = Collections.newSetFromMap(new ConcurrentHashMap<Filter, Boolean>());
pendingVersatiles = Collections.newSetFromMap(
new ConcurrentHashMap<ForwardingObjective, Boolean>());
initializePipeline();
}
......@@ -192,7 +185,6 @@ public class SoftRouterPipeline extends AbstractHandlerBehaviour implements Pipe
}
}
private void initializePipeline() {
//Drop rules for both tables
TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
......@@ -239,8 +231,10 @@ public class SoftRouterPipeline extends AbstractHandlerBehaviour implements Pipe
ApplicationId applicationId) {
// This driver only processes filtering criteria defined with switch
// ports as the key
PortCriterion p; EthCriterion e = null; VlanIdCriterion v = null;
Collection<IPCriterion> ips = new ArrayList<IPCriterion>();
PortCriterion p;
EthCriterion e = null;
VlanIdCriterion v = null;
if (!filt.key().equals(Criteria.dummy()) &&
filt.key().type() == Criterion.Type.IN_PORT) {
p = (PortCriterion) filt.key();
......@@ -258,8 +252,6 @@ public class SoftRouterPipeline extends AbstractHandlerBehaviour implements Pipe
e = (EthCriterion) c;
} else if (c.type() == Criterion.Type.VLAN_VID) {
v = (VlanIdCriterion) c;
} else if (c.type() == Criterion.Type.IPV4_DST) {
ips.add((IPCriterion) c);
} else {
log.error("Unsupported filter {}", c);
fail(filt, ObjectiveError.UNSUPPORTED);
......@@ -272,13 +264,14 @@ public class SoftRouterPipeline extends AbstractHandlerBehaviour implements Pipe
TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
selector.matchInPort(p.port());
selector.matchVlanId(v.vlanId());
selector.matchEthDst(e.mac());
selector.matchVlanId(v.vlanId());
selector.matchEthType(Ethernet.TYPE_IPV4);
if (!v.vlanId().equals(VlanId.NONE)) {
treatment.popVlan();
}
treatment.transition(FIB_TABLE); // all other IPs to the FIB table
treatment.transition(FIB_TABLE);
FlowRule rule = DefaultFlowRule.builder()
.forDevice(deviceId)
.withSelector(selector.build())
......@@ -289,38 +282,6 @@ public class SoftRouterPipeline extends AbstractHandlerBehaviour implements Pipe
.forTable(FILTER_TABLE).build();
ops = ops.add(rule);
for (IPCriterion ipaddr : ips) {
log.debug("adding IP filtering rules in FILTER table: {}", ipaddr.ip());
selector = DefaultTrafficSelector.builder();
treatment = DefaultTrafficTreatment.builder();
selector.matchInPort(p.port());
selector.matchVlanId(v.vlanId());
selector.matchEthDst(e.mac());
selector.matchEthType(Ethernet.TYPE_IPV4);
selector.matchIPDst(ipaddr.ip()); // router IPs to the controller
treatment.setOutput(PortNumber.CONTROLLER);
rule = DefaultFlowRule.builder()
.forDevice(deviceId)
.withSelector(selector.build())
.withTreatment(treatment.build())
.withPriority(HIGHEST_PRIORITY)
.fromApp(applicationId)
.makePermanent()
.forTable(FILTER_TABLE).build();
ops = ops.add(rule);
}
// cache for later use
Filter filter = new Filter(p, e, v, ips);
filters.add(filter);
// apply any pending versatile forwarding objectives
for (ForwardingObjective fwd : pendingVersatiles) {
Collection<FlowRule> ret = processVersatilesWithFilters(filter, fwd);
for (FlowRule fr : ret) {
ops.add(fr);
}
}
ops = install ? ops.add(rule) : ops.remove(rule);
// apply filtering flow rules
flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
......@@ -360,69 +321,20 @@ public class SoftRouterPipeline extends AbstractHandlerBehaviour implements Pipe
* subsystem. May return empty collection in case of failures.
*/
private Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
if (filters.isEmpty()) {
pendingVersatiles.add(fwd);
return Collections.emptySet();
}
Collection<FlowRule> flowrules = new ArrayList<FlowRule>();
for (Filter filter : filters) {
flowrules.addAll(processVersatilesWithFilters(filter, fwd));
}
return flowrules;
}
Collection<FlowRule> flowrules = new ArrayList<>();
private Collection<FlowRule> processVersatilesWithFilters(
Filter filt, ForwardingObjective fwd) {
log.info("Processing versatile forwarding objective");
Collection<FlowRule> flows = new ArrayList<FlowRule>();
TrafficSelector match = fwd.selector();
EthTypeCriterion ethType =
(EthTypeCriterion) match.getCriterion(Criterion.Type.ETH_TYPE);
if (ethType == null) {
log.error("Versatile forwarding objective must include ethType");
fail(fwd, ObjectiveError.UNKNOWN);
return Collections.emptySet();
}
FlowRule rule = DefaultFlowRule.builder()
.withSelector(fwd.selector())
.withTreatment(fwd.treatment())
.makePermanent()
.forDevice(deviceId)
.fromApp(fwd.appId())
.withPriority(fwd.priority())
.build();
if (ethType.ethType().toShort() == Ethernet.TYPE_ARP) {
// need to install ARP request & reply flow rules for each interface filter
// rule for ARP replies
TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
selector.matchInPort(filt.port());
selector.matchVlanId(filt.vlanId());
selector.matchEthDst(filt.mac());
selector.matchEthType(Ethernet.TYPE_ARP);
FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
.fromApp(fwd.appId())
.withPriority(fwd.priority())
.forDevice(deviceId)
.withSelector(selector.build())
.withTreatment(fwd.treatment())
.makePermanent()
.forTable(FILTER_TABLE);
flows.add(ruleBuilder.build());
//rule for ARP requests
selector = DefaultTrafficSelector.builder();
selector.matchInPort(filt.port());
selector.matchVlanId(filt.vlanId());
selector.matchEthDst(MacAddress.BROADCAST);
selector.matchEthType(Ethernet.TYPE_ARP);
ruleBuilder = DefaultFlowRule.builder()
.fromApp(fwd.appId())
.withPriority(fwd.priority())
.forDevice(deviceId)
.withSelector(selector.build())
.withTreatment(fwd.treatment())
.makePermanent()
.forTable(FILTER_TABLE);
flows.add(ruleBuilder.build());
return flows;
}
// not handling other versatile flows
return Collections.emptySet();
flowrules.add(rule);
return flowrules;
}
/**
......@@ -500,35 +412,6 @@ public class SoftRouterPipeline extends AbstractHandlerBehaviour implements Pipe
new DummyGroup(treatment));
}
private class Filter {
private PortCriterion port;
private VlanIdCriterion vlan;
private EthCriterion eth;
@SuppressWarnings("unused")
private Collection<IPCriterion> ips;
public Filter(PortCriterion p, EthCriterion e, VlanIdCriterion v,
Collection<IPCriterion> ips) {
this.eth = e;
this.port = p;
this.vlan = v;
this.ips = ips;
}
public PortNumber port() {
return port.port();
}
public VlanId vlanId() {
return vlan.vlanId();
}
public MacAddress mac() {
return eth.mac();
}
}
private class DummyGroup implements NextGroup {
TrafficTreatment nextActions;
......
......@@ -98,7 +98,7 @@ public class ComponentManager implements ComponentService {
org.apache.felix.scr.Component component = components[0];
if (component.getState() != org.apache.felix.scr.Component.STATE_ACTIVE) {
if (component.getState() == org.apache.felix.scr.Component.STATE_DISABLED) {
log.info("Enabling component {}", name);
component.enable();
}
......