blob: c4dc2b4de3128415edf856390c3e851fdd9cdd7e [file] [log] [blame]
Andrea Campanella378e21a2017-06-07 12:09:59 +02001/*
2 * Copyright 2017-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
Frank Wangc672c982017-07-18 16:09:28 +080019import org.onlab.util.ImmutableByteSequence;
20import org.onosproject.net.Device;
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.device.DeviceService;
Andrea Campanella378e21a2017-06-07 12:09:59 +020023import org.onosproject.net.driver.AbstractHandlerBehaviour;
Andrea Campanella378e21a2017-06-07 12:09:59 +020024import org.onosproject.net.packet.OutboundPacket;
25import org.onosproject.net.packet.PacketProgrammable;
Frank Wangc672c982017-07-18 16:09:28 +080026import org.onosproject.net.pi.model.PiPipeconf;
27import org.onosproject.net.pi.model.PiPipelineInterpreter;
28import org.onosproject.net.pi.runtime.PiPacketOperation;
29import org.onosproject.net.pi.runtime.PiPipeconfService;
30import org.onosproject.p4runtime.api.P4RuntimeClient;
31import org.onosproject.p4runtime.api.P4RuntimeController;
32import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
34
35import static org.onosproject.net.pi.runtime.PiPacketOperation.Type.PACKET_OUT;
Andrea Campanella378e21a2017-06-07 12:09:59 +020036
Andrea Campanella378e21a2017-06-07 12:09:59 +020037/**
38 * Packet Programmable behaviour for BMv2 devices.
39 */
40public class Bmv2PacketProgrammable extends AbstractHandlerBehaviour implements PacketProgrammable {
Frank Wangc672c982017-07-18 16:09:28 +080041 private final Logger log = LoggerFactory.getLogger(getClass());
Carmelo Cascone59f57de2017-07-11 19:55:09 -040042
Andrea Campanella378e21a2017-06-07 12:09:59 +020043 @Override
44 public void emit(OutboundPacket packet) {
Frank Wangc672c982017-07-18 16:09:28 +080045
46 DeviceId deviceId = handler().data().deviceId();
47 P4RuntimeController controller = handler().get(P4RuntimeController.class);
48 if (!controller.hasClient(deviceId)) {
49 log.warn("Unable to find client for {}, aborting the sending packet", deviceId);
50 return;
51 }
52
53 P4RuntimeClient client = controller.getClient(deviceId);
54 PiPipeconfService piPipeconfService = handler().get(PiPipeconfService.class);
55
56 final PiPipeconf pipeconf;
57 if (piPipeconfService.ofDevice(deviceId).isPresent() &&
58 piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).isPresent()) {
59 pipeconf = piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).get();
60 } else {
61 log.warn("Unable to get the pipeconf of the {}", deviceId);
62 return;
63 }
64
65 DeviceService deviceService = handler().get(DeviceService.class);
66 Device device = deviceService.getDevice(deviceId);
67 final PiPipelineInterpreter interpreter = device.is(PiPipelineInterpreter.class)
68 ? device.as(PiPipelineInterpreter.class) : null;
69 if (interpreter == null) {
70 log.warn("Device {} unable to instantiate interpreter of pipeconf {}", deviceId, pipeconf.id());
71 return;
72 }
73
74 try {
75 PiPacketOperation piPacketOperation = PiPacketOperation
76 .builder()
77 .withType(PACKET_OUT)
78 .withData(ImmutableByteSequence.copyFrom(packet.data()))
79 .withMetadatas(interpreter.mapOutboundPacket(packet, pipeconf))
80 .build();
81 client.packetOut(piPacketOperation, pipeconf);
82 } catch (PiPipelineInterpreter.PiInterpreterException e) {
83 log.error("Interpreter of pipeconf {} was unable to translate outbound packet: {}",
84 pipeconf.id(), e.getMessage());
85 }
Andrea Campanella378e21a2017-06-07 12:09:59 +020086 }
87}