blob: 49f0f4bf333ab489583978b996a708064356a5be [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.onosproject.net.Device;
20import org.onosproject.net.DeviceId;
21import org.onosproject.net.device.DeviceService;
Andrea Campanella378e21a2017-06-07 12:09:59 +020022import org.onosproject.net.driver.AbstractHandlerBehaviour;
Andrea Campanella378e21a2017-06-07 12:09:59 +020023import org.onosproject.net.packet.OutboundPacket;
24import org.onosproject.net.packet.PacketProgrammable;
Frank Wangc672c982017-07-18 16:09:28 +080025import org.onosproject.net.pi.model.PiPipeconf;
26import org.onosproject.net.pi.model.PiPipelineInterpreter;
27import org.onosproject.net.pi.runtime.PiPacketOperation;
28import org.onosproject.net.pi.runtime.PiPipeconfService;
29import org.onosproject.p4runtime.api.P4RuntimeClient;
30import org.onosproject.p4runtime.api.P4RuntimeController;
31import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
33
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020034import java.util.Collection;
Andrea Campanella378e21a2017-06-07 12:09:59 +020035
Andrea Campanella378e21a2017-06-07 12:09:59 +020036/**
37 * Packet Programmable behaviour for BMv2 devices.
38 */
39public class Bmv2PacketProgrammable extends AbstractHandlerBehaviour implements PacketProgrammable {
Frank Wangc672c982017-07-18 16:09:28 +080040 private final Logger log = LoggerFactory.getLogger(getClass());
Carmelo Cascone59f57de2017-07-11 19:55:09 -040041
Andrea Campanella378e21a2017-06-07 12:09:59 +020042 @Override
43 public void emit(OutboundPacket packet) {
Frank Wangc672c982017-07-18 16:09:28 +080044
45 DeviceId deviceId = handler().data().deviceId();
46 P4RuntimeController controller = handler().get(P4RuntimeController.class);
47 if (!controller.hasClient(deviceId)) {
48 log.warn("Unable to find client for {}, aborting the sending packet", deviceId);
49 return;
50 }
51
52 P4RuntimeClient client = controller.getClient(deviceId);
53 PiPipeconfService piPipeconfService = handler().get(PiPipeconfService.class);
54
55 final PiPipeconf pipeconf;
56 if (piPipeconfService.ofDevice(deviceId).isPresent() &&
57 piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).isPresent()) {
58 pipeconf = piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).get();
59 } else {
60 log.warn("Unable to get the pipeconf of the {}", deviceId);
61 return;
62 }
63
64 DeviceService deviceService = handler().get(DeviceService.class);
65 Device device = deviceService.getDevice(deviceId);
66 final PiPipelineInterpreter interpreter = device.is(PiPipelineInterpreter.class)
67 ? device.as(PiPipelineInterpreter.class) : null;
68 if (interpreter == null) {
69 log.warn("Device {} unable to instantiate interpreter of pipeconf {}", deviceId, pipeconf.id());
70 return;
71 }
72
73 try {
Carmelo Casconef3a1a382017-07-27 12:04:39 -040074 Collection<PiPacketOperation> operations = interpreter.mapOutboundPacket(packet);
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020075 operations.forEach(piPacketOperation -> {
76 client.packetOut(piPacketOperation, pipeconf);
77 });
Frank Wangc672c982017-07-18 16:09:28 +080078 } catch (PiPipelineInterpreter.PiInterpreterException e) {
79 log.error("Interpreter of pipeconf {} was unable to translate outbound packet: {}",
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020080 pipeconf.id(), e.getMessage());
Frank Wangc672c982017-07-18 16:09:28 +080081 }
Andrea Campanella378e21a2017-06-07 12:09:59 +020082 }
83}