use of org.onosproject.net.flow.TrafficTreatment in project trellis-control by opennetworkinglab.
the class MockFlowObjectiveService method forward.
@Override
public void forward(DeviceId deviceId, ForwardingObjective forwardingObjective) {
TrafficSelector selector = forwardingObjective.selector();
TrafficTreatment treatment = nextTable.get(forwardingObjective.nextId());
MacAddress macAddress = ((EthCriterion) selector.getCriterion(Criterion.Type.ETH_DST)).mac();
VlanId vlanId = ((VlanIdCriterion) selector.getCriterion(Criterion.Type.VLAN_VID)).vlanId();
boolean popVlan = treatment.allInstructions().stream().filter(instruction -> instruction.type().equals(Instruction.Type.L2MODIFICATION)).anyMatch(instruction -> ((L2ModificationInstruction) instruction).subtype().equals(L2ModificationInstruction.L2SubType.VLAN_POP));
PortNumber portNumber = treatment.allInstructions().stream().filter(instruction -> instruction.type().equals(Instruction.Type.OUTPUT)).map(instruction -> ((Instructions.OutputInstruction) instruction).port()).findFirst().orElse(null);
if (portNumber == null) {
throw new IllegalArgumentException();
}
Objective.Operation op = forwardingObjective.op();
MockBridgingTableKey btKey = new MockBridgingTableKey(deviceId, macAddress, vlanId);
MockBridgingTableValue btValue = new MockBridgingTableValue(popVlan, portNumber);
if (op.equals(Objective.Operation.ADD)) {
bridgingTable.put(btKey, btValue);
forwardingObjective.context().ifPresent(context -> context.onSuccess(forwardingObjective));
} else if (op.equals(Objective.Operation.REMOVE)) {
bridgingTable.remove(btKey, btValue);
forwardingObjective.context().ifPresent(context -> context.onSuccess(forwardingObjective));
} else {
forwardingObjective.context().ifPresent(context -> context.onError(forwardingObjective, ObjectiveError.UNKNOWN));
throw new IllegalArgumentException();
}
}
use of org.onosproject.net.flow.TrafficTreatment in project trellis-control by opennetworkinglab.
the class PolicyManager method installTrafficMatchToDevice.
// Orchestrate traffic match installation according to the type
private void installTrafficMatchToDevice(DeviceId deviceId, TrafficMatch trafficMatch) {
if (log.isDebugEnabled()) {
log.debug("Installing traffic match {} associated to policy {}", trafficMatch.trafficMatchId(), trafficMatch.policyId());
}
TrafficMatchKey trafficMatchKey = new TrafficMatchKey(deviceId, trafficMatch.trafficMatchId());
Operation oldTrafficOperation = Versioned.valueOrNull(operations.get(trafficMatchKey.toString()));
if (oldTrafficOperation != null && oldTrafficOperation.isInstall()) {
if (trafficMatch.equals(oldTrafficOperation.trafficMatch().orElse(null))) {
if (log.isDebugEnabled()) {
log.debug("There is already an install operation for traffic match {} associated to policy {} " + "for device {}", trafficMatch.trafficMatchId(), trafficMatch.policyId(), deviceId);
}
// If we add or submit a trafficMatch multiple times
// We skip the installation and update the state directly
updateTrafficMatch(trafficMatch, true);
return;
} else {
if (log.isDebugEnabled()) {
log.debug("Starts updating traffic match {} associated to policy {} " + "for device {}", trafficMatch.trafficMatchId(), trafficMatch.policyId(), deviceId);
}
}
}
// For the DROP policy we need to set an ACL drop in the fwd objective. The other
// policies require to retrieve the next Id and sets the next step.
PolicyKey policyKey = new PolicyKey(deviceId, trafficMatch.policyId());
Operation policyOperation = Versioned.valueOrNull(operations.get(policyKey.toString()));
if (policyOperation == null || !policyOperation.isDone() || !policyOperation.isInstall() || policyOperation.policy().isEmpty() || (policyOperation.policy().get().policyType() == PolicyType.REDIRECT && policyOperation.objectiveOperation() == null)) {
log.info("Deferring traffic match {} installation on device {}. Policy {} not yet installed", trafficMatch.trafficMatchId(), deviceId, trafficMatch.policyId());
return;
}
// Updates the store and then send the versatile fwd objective to the pipeliner
Operation newTrafficOperation = Operation.builder().isInstall(true).trafficMatch(trafficMatch).build();
operations.put(trafficMatchKey.toString(), newTrafficOperation);
Policy policy = policyOperation.policy().get();
ForwardingObjective.Builder builder = trafficMatchFwdObjective(trafficMatch, policy.policyType());
if (policy.policyType() == PolicyType.DROP) {
// Firstly builds the fwd objective with the wipeDeferred action.
TrafficTreatment dropTreatment = DefaultTrafficTreatment.builder().wipeDeferred().build();
builder.withTreatment(dropTreatment);
} else if (policy.policyType() == PolicyType.REDIRECT) {
// Here we need to set only the next step
builder.nextStep(policyOperation.objectiveOperation().id());
}
// Once, the fwd objective has completed its execution, we update the policiesOps map
CompletableFuture<Objective> addNewFuture = new CompletableFuture<>();
CompletableFuture<Objective> removeOldFuture = new CompletableFuture<>();
if (log.isDebugEnabled()) {
log.debug("Installing forwarding objective for dev: {}", deviceId);
}
ObjectiveContext addNewContext = new DefaultObjectiveContext((objective) -> {
if (log.isDebugEnabled()) {
log.debug("Forwarding objective for policy {} installed", trafficMatch.policyId());
}
addNewFuture.complete(objective);
}, (objective, error) -> {
log.warn("Failed to install forwarding objective for policy {}: {}", trafficMatch.policyId(), error);
addNewFuture.complete(null);
});
ObjectiveContext removeOldContext = new DefaultObjectiveContext((objective) -> {
if (log.isDebugEnabled()) {
log.debug("Old forwarding objective for policy {} removed, update finished", trafficMatch.policyId());
}
removeOldFuture.complete(objective);
}, (objective, error) -> {
log.warn("Failed to remove old forwarding objective for policy {}: {}", trafficMatch.policyId(), error);
removeOldFuture.complete(null);
});
// Context is not serializable
ForwardingObjective serializableObjective = builder.add();
flowObjectiveService.forward(deviceId, builder.add(addNewContext));
addNewFuture.whenComplete((objective, ex) -> {
if (ex != null) {
log.error("Exception installing forwarding objective", ex);
} else if (objective != null) {
// base on priority, selector and metadata change
if (oldTrafficOperation != null && oldTrafficOperation.objectiveOperation() != null && oldTrafficOperation.isInstall() && (oldTrafficOperation.objectiveOperation().priority() != serializableObjective.priority() || !((ForwardingObjective) oldTrafficOperation.objectiveOperation()).selector().equals(serializableObjective.selector()) || !((ForwardingObjective) oldTrafficOperation.objectiveOperation()).meta().equals(serializableObjective.meta()))) {
ForwardingObjective oldFwdObj = (ForwardingObjective) oldTrafficOperation.objectiveOperation();
ForwardingObjective.Builder oldBuilder = DefaultForwardingObjective.builder(oldFwdObj);
flowObjectiveService.forward(deviceId, oldBuilder.remove(removeOldContext));
} else {
operations.computeIfPresent(trafficMatchKey.toString(), (k, v) -> {
if (!v.isDone() && v.isInstall()) {
v.isDone(true);
v.objectiveOperation(serializableObjective);
}
return v;
});
}
}
});
removeOldFuture.whenComplete((objective, ex) -> {
if (ex != null) {
log.error("Exception removing old forwarding objective", ex);
} else if (objective != null) {
operations.computeIfPresent(trafficMatchKey.toString(), (k, v) -> {
if (!v.isDone() && v.isInstall()) {
v.isDone(true);
v.objectiveOperation(serializableObjective);
}
return v;
});
}
});
}
use of org.onosproject.net.flow.TrafficTreatment in project ddosdn by ssulca.
the class ReactivePacketProcessor method forwardPacketToDst.
/**
* Realiza el reenvio de paquetes
* @param context contexto del paquete
* @param dst host del destino
*/
private void forwardPacketToDst(PacketContext context, Host dst) {
TrafficTreatment treatment;
OutboundPacket packet;
treatment = DefaultTrafficTreatment.builder().setOutput(dst.location().port()).build();
packet = new DefaultOutboundPacket(dst.location().deviceId(), treatment, context.inPacket().unparsed());
// Entrega paquete, tarea realizada por el controlador.
this.packetService.emit(packet);
}
use of org.onosproject.net.flow.TrafficTreatment in project ddosdn by ssulca.
the class ReactivePacketProcessor method setUpConnectivity.
/**
* Install a rule forwarding the packet to the specified port. connex 1:N
* @param srcCp source connect point
* @param dstCp dest connect point
*/
public Key setUpConnectivity(FilteredConnectPoint srcCp, FilteredConnectPoint dstCp, Host idsHost, boolean duplicateTraffic) {
int priority;
Key key;
Host ids;
String idsKeyString;
TrafficSelector idsSelector;
TrafficSelector selector;
TrafficTreatment treatment;
Set<FilteredConnectPoint> egressPoints;
FilteredConnectPoint filterIdsCp;
idsKeyString = "";
egressPoints = new HashSet<>();
priority = intentNormalPri;
selector = DefaultTrafficSelector.emptySelector();
treatment = DefaultTrafficTreatment.emptyTreatment();
// Do Acction in Mutex enviroment
try {
semaphore.acquire();
} catch (InterruptedException e) {
log.error("Semaforo Dont Acacquire");
return null;
}
// Add initial dst
egressPoints.add(dstCp);
// If DoS/DDoS case, Add IDS dst.
if (duplicateTraffic) {
idsSelector = DefaultTrafficSelector.builder().matchEthType(Ethernet.TYPE_IPV4).build();
// ids = findIds(dstCp);
// Find The nearest IDS to srcCp
// ids = findIds(srcCp);
filterIdsCp = new FilteredConnectPoint(idsHost.location(), idsSelector);
// Add The nearest IDS
egressPoints.add(filterIdsCp);
priority = intentDDoslPri;
idsKeyString = filterIdsCp.toString();
}
key = (srcCp.toString().compareTo(dstCp.toString()) < 0) ? // True
Key.of(srcCp.toString() + dstCp.toString() + idsKeyString, appId) : // False
Key.of(dstCp.toString() + srcCp.toString() + idsKeyString, appId);
intentKeys.add(key);
if (intentService.getIntent(key) != null) {
if (WITHDRAWN_STATES.contains(intentService.getIntentState(key))) {
buildIntent(key, srcCp, egressPoints, priority, selector, treatment);
}
} else {
buildIntent(key, srcCp, egressPoints, priority, selector, treatment);
}
semaphore.release();
// retorna la clave del ultimo intent creado.
return key;
}
use of org.onosproject.net.flow.TrafficTreatment in project TFG by mattinelorza.
the class InterpreterImpl method mapOutboundPacket.
/**
* Returns a collection of PI packet operations populated with metadata
* specific for this pipeconf and equivalent to the given ONOS
* OutboundPacket instance.
*
* @param packet ONOS OutboundPacket
* @return collection of PI packet operations
* @throws PiInterpreterException if the packet treatments cannot be
* executed by this pipeline
*/
@Override
public Collection<PiPacketOperation> mapOutboundPacket(OutboundPacket packet) throws PiInterpreterException {
TrafficTreatment treatment = packet.treatment();
// Packet-out in main.p4 supports only setting the output port,
// i.e. we only understand OUTPUT instructions.
List<OutputInstruction> outInstructions = treatment.allInstructions().stream().filter(i -> i.type().equals(OUTPUT)).map(i -> (OutputInstruction) i).collect(toList());
if (treatment.allInstructions().size() != outInstructions.size()) {
// There are other instructions that are not of type OUTPUT.
throw new PiInterpreterException("Treatment not supported: " + treatment);
}
ImmutableList.Builder<PiPacketOperation> builder = ImmutableList.builder();
for (OutputInstruction outInst : outInstructions) {
if (outInst.port().isLogical() && !outInst.port().equals(FLOOD)) {
throw new PiInterpreterException(format("Packet-out on logical port '%s' not supported", outInst.port()));
} else if (outInst.port().equals(FLOOD)) {
// To emulate flooding, we create a packet-out operation for
// each switch port.
final DeviceService deviceService = handler().get(DeviceService.class);
for (Port port : deviceService.getPorts(packet.sendThrough())) {
builder.add(buildPacketOut(packet.data(), port.number().toLong()));
}
} else {
// Create only one packet-out for the given OUTPUT instruction.
builder.add(buildPacketOut(packet.data(), outInst.port().toLong()));
}
}
return builder.build();
}
Aggregations