blob: 24add741251598ee5368f7ec16ed8067ba57f8dc [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
19import org.onlab.util.ImmutableByteSequence;
20import org.onosproject.bmv2.api.runtime.Bmv2Client;
21import org.onosproject.bmv2.api.runtime.Bmv2RuntimeException;
22import org.onosproject.bmv2.ctl.Bmv2ThriftClient;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.driver.AbstractHandlerBehaviour;
25import org.onosproject.net.flow.TrafficTreatment;
26import org.onosproject.net.flow.instructions.Instructions;
27import org.onosproject.net.packet.OutboundPacket;
28import org.onosproject.net.packet.PacketProgrammable;
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32import static java.lang.Math.toIntExact;
33import static org.onosproject.net.PortNumber.FLOOD;
34import static org.onosproject.net.flow.instructions.Instruction.Type.OUTPUT;
35
36/**
37 * Packet programmable device behaviour implementation for BMv2.
38 */
39public class Bmv2PacketProgrammable extends AbstractHandlerBehaviour implements PacketProgrammable {
40
41 private static final Logger LOG =
Carmelo Casconea2f510e2016-05-03 18:36:45 -070042 LoggerFactory.getLogger(Bmv2PacketProgrammable.class);
Carmelo Casconee9121642016-04-27 17:02:38 -070043
44 @Override
45 public void emit(OutboundPacket packet) {
46
47 TrafficTreatment treatment = packet.treatment();
48
49 treatment.allInstructions().forEach(inst -> {
50 if (inst.type().equals(OUTPUT)) {
51 Instructions.OutputInstruction outInst = (Instructions.OutputInstruction) inst;
52 if (outInst.port().isLogical()) {
53 if (outInst.port() == FLOOD) {
54 // TODO: implement flood
55 LOG.info("Flood not implemented", outInst);
56 }
57 LOG.info("Output on logical port not supported: {}", outInst);
58 } else {
59 try {
60 long longPort = outInst.port().toLong();
61 int portNumber = toIntExact(longPort);
62 send(portNumber, packet);
63 } catch (ArithmeticException e) {
64 LOG.error("Port number overflow! Cannot send packet on port {} (long), as the bmv2" +
65 " device only accepts int port values.");
66 }
67 }
68 } else {
69 LOG.info("Instruction type not supported: {}", inst.type().name());
70 }
71 });
72 }
73
74 private void send(int port, OutboundPacket packet) {
75
76 DeviceId deviceId = handler().data().deviceId();
77
78 Bmv2Client deviceClient;
79 try {
80 deviceClient = Bmv2ThriftClient.of(deviceId);
81 } catch (Bmv2RuntimeException e) {
82 LOG.error("Failed to connect to Bmv2 device", e);
83 return;
84 }
85
86 ImmutableByteSequence bs = ImmutableByteSequence.copyFrom(packet.data());
87 try {
88 deviceClient.transmitPacket(port, bs);
89 } catch (Bmv2RuntimeException e) {
90 LOG.info("Unable to push packet to device: deviceId={}, packet={}", deviceId, bs);
91 }
92 }
93}