blob: 5752bfa472c5f96c2f89096609f01dce88789945 [file] [log] [blame]
Pier Ventre10bd8d12016-11-26 21:05:22 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Pier Ventre10bd8d12016-11-26 21:05:22 -08003 *
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.segmentrouting;
18
Pier Ventre735b8c82016-12-02 08:16:05 -080019import org.onlab.packet.Ethernet;
20import org.onlab.packet.IpAddress;
21import org.onlab.packet.MacAddress;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070022import org.onlab.packet.VlanId;
Ray Milkeyb65d7842017-08-03 16:28:24 -070023import org.onosproject.net.neighbour.NeighbourMessageContext;
Pier Ventre735b8c82016-12-02 08:16:05 -080024import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.Host;
27import org.onosproject.net.HostId;
28import org.onosproject.net.flow.DefaultTrafficTreatment;
29import org.onosproject.net.flow.TrafficTreatment;
Pier Ventre10bd8d12016-11-26 21:05:22 -080030import org.onosproject.net.host.HostService;
Pier Ventre735b8c82016-12-02 08:16:05 -080031import org.onosproject.net.packet.DefaultOutboundPacket;
32import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
33import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventre10bd8d12016-11-26 21:05:22 -080034import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
36
Pier Ventre735b8c82016-12-02 08:16:05 -080037import java.nio.ByteBuffer;
38
39import static com.google.common.base.Preconditions.checkNotNull;
40
Pier Ventre10bd8d12016-11-26 21:05:22 -080041/**
Pier Ventre735b8c82016-12-02 08:16:05 -080042 * This handler provides provides useful functions to the
43 * neighbour handlers (ARP, NDP).
Pier Ventre10bd8d12016-11-26 21:05:22 -080044 */
Pier Ventre735b8c82016-12-02 08:16:05 -080045public class SegmentRoutingNeighbourHandler {
Pier Ventre10bd8d12016-11-26 21:05:22 -080046
47 private static Logger log = LoggerFactory.getLogger(SegmentRoutingNeighbourHandler.class);
Pier Ventre735b8c82016-12-02 08:16:05 -080048
49 protected SegmentRoutingManager srManager;
50 protected DeviceConfiguration config;
Pier Ventre10bd8d12016-11-26 21:05:22 -080051
52 /**
Pier Ventre735b8c82016-12-02 08:16:05 -080053 * Creates an SegmentRoutingNeighbourHandler object.
Pier Ventre10bd8d12016-11-26 21:05:22 -080054 *
Pier Ventre735b8c82016-12-02 08:16:05 -080055 * @param srManager SegmentRoutingManager object
Pier Ventre10bd8d12016-11-26 21:05:22 -080056 */
Pier Ventre735b8c82016-12-02 08:16:05 -080057 public SegmentRoutingNeighbourHandler(SegmentRoutingManager srManager) {
58 this.srManager = srManager;
59 this.config = checkNotNull(srManager.deviceConfiguration);
Pier Ventre10bd8d12016-11-26 21:05:22 -080060 }
61
Pier Ventre735b8c82016-12-02 08:16:05 -080062 /**
63 * Creates an SegmentRoutingNeighbourHandler object.
Pier Ventre10bd8d12016-11-26 21:05:22 -080064 */
Pier Ventre735b8c82016-12-02 08:16:05 -080065 public SegmentRoutingNeighbourHandler() {
66 this.srManager = null;
67 this.config = null;
68 }
69
70 /**
71 * Retrieve router (device) info.
72 *
73 * @param mac where to copy the mac
74 * @param ip where to copy the ip
75 * @param deviceId the device id
76 * @param targetAddress the target address
Pier Luigia905c0c2017-01-29 12:38:48 -080077 * @return true if it was possible to get the necessary info.
78 * False for errors
Pier Ventre735b8c82016-12-02 08:16:05 -080079 */
Pier Luigia905c0c2017-01-29 12:38:48 -080080 protected boolean getSenderInfo(byte[] mac,
Pier Ventre735b8c82016-12-02 08:16:05 -080081 byte[] ip,
82 DeviceId deviceId,
83 IpAddress targetAddress) {
84 byte[] senderMacAddress;
85 byte[] senderIpAddress;
Pier Luigia905c0c2017-01-29 12:38:48 -080086 IpAddress sender;
Pier Ventre735b8c82016-12-02 08:16:05 -080087 try {
88 senderMacAddress = config.getDeviceMac(deviceId).toBytes();
Pier Ventre968da122016-12-09 17:26:04 -080089 if (targetAddress.isIp4()) {
Pier Luigia905c0c2017-01-29 12:38:48 -080090 sender = config.getRouterIpAddressForASubnetHost(targetAddress.getIp4Address());
Pier Ventre968da122016-12-09 17:26:04 -080091 } else {
Pier Luigia905c0c2017-01-29 12:38:48 -080092 sender = config.getRouterIpAddressForASubnetHost(targetAddress.getIp6Address());
Pier Ventre968da122016-12-09 17:26:04 -080093 }
Pier Luigia905c0c2017-01-29 12:38:48 -080094 // If sender is null we abort.
95 if (sender == null) {
96 log.warn("Sender ip is null. Aborting getSenderInfo");
97 return false;
98 }
99 senderIpAddress = sender.toOctets();
Pier Ventre735b8c82016-12-02 08:16:05 -0800100 } catch (DeviceConfigNotFoundException e) {
Pier Luigia905c0c2017-01-29 12:38:48 -0800101 log.warn(e.getMessage() + " Aborting getSenderInfo");
102 return false;
Pier Ventre735b8c82016-12-02 08:16:05 -0800103 }
Pier Ventre735b8c82016-12-02 08:16:05 -0800104 System.arraycopy(senderMacAddress, 0, mac, 0, senderMacAddress.length);
105 System.arraycopy(senderIpAddress, 0, ip, 0, senderIpAddress.length);
Pier Luigia905c0c2017-01-29 12:38:48 -0800106 return true;
Pier Ventre735b8c82016-12-02 08:16:05 -0800107 }
108
109 /**
110 * Utility to send a ND reply using the supplied information.
111 *
112 * @param pkt the request
113 * @param targetMac the target mac
114 * @param hostService the host service
115 */
116 protected void sendResponse(NeighbourMessageContext pkt, MacAddress targetMac, HostService hostService) {
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700117 short vlanId = pkt.packet().getQinQVID();
118 HostId dstId = HostId.hostId(pkt.srcMac(), vlanId == Ethernet.VLAN_UNTAGGED
119 ? pkt.vlan() : VlanId.vlanId(vlanId));
Pier Ventre735b8c82016-12-02 08:16:05 -0800120 Host dst = hostService.getHost(dstId);
121 if (dst == null) {
122 log.warn("Cannot send {} response to host {} - does not exist in the store",
123 pkt.protocol(), dstId);
124 return;
125 }
126 pkt.reply(targetMac);
127 }
128
129 /**
130 * Flood to all ports in the same subnet.
131 *
132 * @param packet packet to be flooded
133 * @param inPort where the packet comes from
134 * @param targetAddress the target address
135 */
136 protected void flood(Ethernet packet, ConnectPoint inPort, IpAddress targetAddress) {
137 try {
138 srManager.deviceConfiguration
139 .getSubnetPortsMap(inPort.deviceId()).forEach((subnet, ports) -> {
140 if (subnet.contains(targetAddress)) {
141 ports.stream()
142 .filter(port -> port != inPort.port())
143 .forEach(port -> {
144 forward(packet, new ConnectPoint(inPort.deviceId(), port));
145 });
Pier Ventre10bd8d12016-11-26 21:05:22 -0800146 }
Pier Ventre735b8c82016-12-02 08:16:05 -0800147 });
148 } catch (DeviceConfigNotFoundException e) {
149 log.warn(e.getMessage()
150 + " Cannot flood in subnet as device config not available"
151 + " for device: " + inPort.deviceId());
Pier Ventre10bd8d12016-11-26 21:05:22 -0800152 }
153 }
154
Pier Ventre735b8c82016-12-02 08:16:05 -0800155 /*
156 * Floods only on the port which have been configured with the subnet
157 * of the target address. The in port is excluded.
158 *
159 * @param pkt the ndp/arp packet and context information
160 */
161 protected void flood(NeighbourMessageContext pkt) {
162 try {
163 srManager.deviceConfiguration
164 .getSubnetPortsMap(pkt.inPort().deviceId()).forEach((subnet, ports) -> {
165 if (subnet.contains(pkt.target())) {
166 ports.stream()
167 .filter(port -> port != pkt.inPort().port())
168 .forEach(port -> {
169 ConnectPoint outPoint = new ConnectPoint(
170 pkt.inPort().deviceId(),
171 port
172 );
173 pkt.forward(outPoint);
174 });
175 }
176 });
177 } catch (DeviceConfigNotFoundException e) {
178 log.warn(e.getMessage()
179 + " Cannot flood in subnet as device config not available"
180 + " for device: " + pkt.inPort().deviceId());
181 }
182 }
183
184 /**
185 * Packet out to given port.
186 *
187 * Note: In current implementation, we expect all communication with
188 * end hosts within a subnet to be untagged.
189 * <p>
190 * For those pipelines that internally assigns a VLAN, the VLAN tag will be
191 * removed before egress.
192 * <p>
193 * For those pipelines that do not assign internal VLAN, the packet remains
194 * untagged.
195 *
196 * @param packet packet to be forwarded
197 * @param outPort where the packet should be forwarded
198 */
199 private void forward(Ethernet packet, ConnectPoint outPort) {
200 ByteBuffer buf = ByteBuffer.wrap(packet.serialize());
201
202 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
203 tbuilder.setOutput(outPort.port());
204 srManager.packetService.emit(new DefaultOutboundPacket(outPort.deviceId(),
205 tbuilder.build(), buf));
206 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800207
208}