blob: e04d8757cd2b56aedddfbca55f66441ff5e24801 [file] [log] [blame]
Carmelo Casconee9121642016-04-27 17:02:38 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.drivers.bmv2;
18
Carmelo Casconefbc577b2016-06-17 23:19:09 -070019import org.onlab.osgi.ServiceNotFoundException;
Carmelo Casconee9121642016-04-27 17:02:38 -070020import org.onlab.util.ImmutableByteSequence;
Carmelo Cascone0831efb2016-05-31 14:50:19 -070021import org.onosproject.bmv2.api.runtime.Bmv2DeviceAgent;
Carmelo Casconee9121642016-04-27 17:02:38 -070022import org.onosproject.bmv2.api.runtime.Bmv2RuntimeException;
Carmelo Cascone0831efb2016-05-31 14:50:19 -070023import org.onosproject.bmv2.api.service.Bmv2Controller;
Carmelo Casconee9121642016-04-27 17:02:38 -070024import org.onosproject.net.DeviceId;
25import org.onosproject.net.driver.AbstractHandlerBehaviour;
26import org.onosproject.net.flow.TrafficTreatment;
Carmelo Casconee9121642016-04-27 17:02:38 -070027import org.onosproject.net.packet.OutboundPacket;
28import org.onosproject.net.packet.PacketProgrammable;
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
Carmelo Cascone0831efb2016-05-31 14:50:19 -070032import java.util.List;
33
Carmelo Casconee9121642016-04-27 17:02:38 -070034import static java.lang.Math.toIntExact;
Carmelo Cascone0831efb2016-05-31 14:50:19 -070035import static java.util.stream.Collectors.toList;
Carmelo Casconee9121642016-04-27 17:02:38 -070036import static org.onosproject.net.flow.instructions.Instruction.Type.OUTPUT;
Carmelo Cascone0831efb2016-05-31 14:50:19 -070037import static org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
Carmelo Casconee9121642016-04-27 17:02:38 -070038
39/**
Carmelo Cascone0831efb2016-05-31 14:50:19 -070040 * Implementation of the packet programmable behaviour for BMv2.
Carmelo Casconee9121642016-04-27 17:02:38 -070041 */
42public class Bmv2PacketProgrammable extends AbstractHandlerBehaviour implements PacketProgrammable {
43
Carmelo Cascone0831efb2016-05-31 14:50:19 -070044 private final Logger log = LoggerFactory.getLogger(this.getClass());
Carmelo Casconee9121642016-04-27 17:02:38 -070045
46 @Override
47 public void emit(OutboundPacket packet) {
48
49 TrafficTreatment treatment = packet.treatment();
50
Carmelo Cascone0831efb2016-05-31 14:50:19 -070051 // BMv2 supports only OUTPUT instructions.
52 List<OutputInstruction> outInstructions = treatment.allInstructions()
53 .stream()
54 .filter(i -> i.type().equals(OUTPUT))
55 .map(i -> (OutputInstruction) i)
56 .collect(toList());
57
58 if (treatment.allInstructions().size() != outInstructions.size()) {
59 // There are other instructions that are not of type OUTPUT
60 log.warn("Dropping emit request, treatment nor supported: {}", treatment);
61 return;
62 }
63
64 outInstructions.forEach(outInst -> {
65 if (outInst.port().isLogical()) {
66 log.warn("Dropping emit request, logical port not supported: {}", outInst.port());
Carmelo Casconee9121642016-04-27 17:02:38 -070067 } else {
Carmelo Cascone0831efb2016-05-31 14:50:19 -070068 try {
69 int portNumber = toIntExact(outInst.port().toLong());
70 send(portNumber, packet);
71 } catch (ArithmeticException e) {
72 log.error("Dropping emit request, port number too big: {}", outInst.port().toLong());
73 }
Carmelo Casconee9121642016-04-27 17:02:38 -070074 }
75 });
76 }
77
78 private void send(int port, OutboundPacket packet) {
79
80 DeviceId deviceId = handler().data().deviceId();
81
Carmelo Casconefbc577b2016-06-17 23:19:09 -070082 Bmv2Controller controller;
83 try {
84 controller = handler().get(Bmv2Controller.class);
85 } catch (ServiceNotFoundException e) {
86 log.warn(e.getMessage());
Carmelo Cascone0831efb2016-05-31 14:50:19 -070087 return;
88 }
89
90 Bmv2DeviceAgent deviceAgent;
Carmelo Casconee9121642016-04-27 17:02:38 -070091 try {
Carmelo Cascone0831efb2016-05-31 14:50:19 -070092 deviceAgent = controller.getAgent(deviceId);
Carmelo Casconee9121642016-04-27 17:02:38 -070093 } catch (Bmv2RuntimeException e) {
Carmelo Cascone0831efb2016-05-31 14:50:19 -070094 log.error("Failed to get Bmv2 device agent for {}: {}", deviceId, e.explain());
Carmelo Casconee9121642016-04-27 17:02:38 -070095 return;
96 }
97
98 ImmutableByteSequence bs = ImmutableByteSequence.copyFrom(packet.data());
99 try {
Carmelo Cascone0831efb2016-05-31 14:50:19 -0700100 deviceAgent.transmitPacket(port, bs);
Carmelo Casconee9121642016-04-27 17:02:38 -0700101 } catch (Bmv2RuntimeException e) {
Carmelo Cascone0831efb2016-05-31 14:50:19 -0700102 log.warn("Unable to emit packet trough {}: {}", deviceId, e.explain());
Carmelo Casconee9121642016-04-27 17:02:38 -0700103 }
104 }
105}