Carmelo Cascone | e912164 | 2016-04-27 17:02:38 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.drivers.bmv2; |
| 18 | |
Carmelo Cascone | fbc577b | 2016-06-17 23:19:09 -0700 | [diff] [blame] | 19 | import org.onlab.osgi.ServiceNotFoundException; |
Carmelo Cascone | e912164 | 2016-04-27 17:02:38 -0700 | [diff] [blame] | 20 | import org.onlab.util.ImmutableByteSequence; |
Carmelo Cascone | 0831efb | 2016-05-31 14:50:19 -0700 | [diff] [blame] | 21 | import org.onosproject.bmv2.api.runtime.Bmv2DeviceAgent; |
Carmelo Cascone | e912164 | 2016-04-27 17:02:38 -0700 | [diff] [blame] | 22 | import org.onosproject.bmv2.api.runtime.Bmv2RuntimeException; |
Carmelo Cascone | 0831efb | 2016-05-31 14:50:19 -0700 | [diff] [blame] | 23 | import org.onosproject.bmv2.api.service.Bmv2Controller; |
Carmelo Cascone | e912164 | 2016-04-27 17:02:38 -0700 | [diff] [blame] | 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 26 | import org.onosproject.net.flow.TrafficTreatment; |
Carmelo Cascone | e912164 | 2016-04-27 17:02:38 -0700 | [diff] [blame] | 27 | import org.onosproject.net.packet.OutboundPacket; |
| 28 | import org.onosproject.net.packet.PacketProgrammable; |
| 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
Carmelo Cascone | 0831efb | 2016-05-31 14:50:19 -0700 | [diff] [blame] | 32 | import java.util.List; |
| 33 | |
Carmelo Cascone | e912164 | 2016-04-27 17:02:38 -0700 | [diff] [blame] | 34 | import static java.lang.Math.toIntExact; |
Carmelo Cascone | 0831efb | 2016-05-31 14:50:19 -0700 | [diff] [blame] | 35 | import static java.util.stream.Collectors.toList; |
Carmelo Cascone | e912164 | 2016-04-27 17:02:38 -0700 | [diff] [blame] | 36 | import static org.onosproject.net.flow.instructions.Instruction.Type.OUTPUT; |
Carmelo Cascone | 0831efb | 2016-05-31 14:50:19 -0700 | [diff] [blame] | 37 | import static org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
Carmelo Cascone | e912164 | 2016-04-27 17:02:38 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
Carmelo Cascone | 0831efb | 2016-05-31 14:50:19 -0700 | [diff] [blame] | 40 | * Implementation of the packet programmable behaviour for BMv2. |
Carmelo Cascone | e912164 | 2016-04-27 17:02:38 -0700 | [diff] [blame] | 41 | */ |
| 42 | public class Bmv2PacketProgrammable extends AbstractHandlerBehaviour implements PacketProgrammable { |
| 43 | |
Carmelo Cascone | 0831efb | 2016-05-31 14:50:19 -0700 | [diff] [blame] | 44 | private final Logger log = LoggerFactory.getLogger(this.getClass()); |
Carmelo Cascone | e912164 | 2016-04-27 17:02:38 -0700 | [diff] [blame] | 45 | |
| 46 | @Override |
| 47 | public void emit(OutboundPacket packet) { |
| 48 | |
| 49 | TrafficTreatment treatment = packet.treatment(); |
| 50 | |
Carmelo Cascone | 0831efb | 2016-05-31 14:50:19 -0700 | [diff] [blame] | 51 | // 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 Cascone | e912164 | 2016-04-27 17:02:38 -0700 | [diff] [blame] | 67 | } else { |
Carmelo Cascone | 0831efb | 2016-05-31 14:50:19 -0700 | [diff] [blame] | 68 | 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 Cascone | e912164 | 2016-04-27 17:02:38 -0700 | [diff] [blame] | 74 | } |
| 75 | }); |
| 76 | } |
| 77 | |
| 78 | private void send(int port, OutboundPacket packet) { |
| 79 | |
| 80 | DeviceId deviceId = handler().data().deviceId(); |
| 81 | |
Carmelo Cascone | fbc577b | 2016-06-17 23:19:09 -0700 | [diff] [blame] | 82 | Bmv2Controller controller; |
| 83 | try { |
| 84 | controller = handler().get(Bmv2Controller.class); |
| 85 | } catch (ServiceNotFoundException e) { |
| 86 | log.warn(e.getMessage()); |
Carmelo Cascone | 0831efb | 2016-05-31 14:50:19 -0700 | [diff] [blame] | 87 | return; |
| 88 | } |
| 89 | |
| 90 | Bmv2DeviceAgent deviceAgent; |
Carmelo Cascone | e912164 | 2016-04-27 17:02:38 -0700 | [diff] [blame] | 91 | try { |
Carmelo Cascone | 0831efb | 2016-05-31 14:50:19 -0700 | [diff] [blame] | 92 | deviceAgent = controller.getAgent(deviceId); |
Carmelo Cascone | e912164 | 2016-04-27 17:02:38 -0700 | [diff] [blame] | 93 | } catch (Bmv2RuntimeException e) { |
Carmelo Cascone | 0831efb | 2016-05-31 14:50:19 -0700 | [diff] [blame] | 94 | log.error("Failed to get Bmv2 device agent for {}: {}", deviceId, e.explain()); |
Carmelo Cascone | e912164 | 2016-04-27 17:02:38 -0700 | [diff] [blame] | 95 | return; |
| 96 | } |
| 97 | |
| 98 | ImmutableByteSequence bs = ImmutableByteSequence.copyFrom(packet.data()); |
| 99 | try { |
Carmelo Cascone | 0831efb | 2016-05-31 14:50:19 -0700 | [diff] [blame] | 100 | deviceAgent.transmitPacket(port, bs); |
Carmelo Cascone | e912164 | 2016-04-27 17:02:38 -0700 | [diff] [blame] | 101 | } catch (Bmv2RuntimeException e) { |
Carmelo Cascone | 0831efb | 2016-05-31 14:50:19 -0700 | [diff] [blame] | 102 | log.warn("Unable to emit packet trough {}: {}", deviceId, e.explain()); |
Carmelo Cascone | e912164 | 2016-04-27 17:02:38 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | } |