blob: c1d294832b07bdde04f673fc545e58b367897b77 [file] [log] [blame]
Pier Ventreb6a7f342016-11-26 21:05:22 -08001/*
Brian O'Connor0947d7e2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Pier Ventreb6a7f342016-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 Ventreb6b81d52016-12-02 08:16:05 -080019import org.onlab.packet.Ethernet;
20import org.onlab.packet.IpAddress;
21import org.onlab.packet.MacAddress;
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -070022import org.onlab.packet.VlanId;
Ray Milkey2a31aeb2017-08-03 16:28:24 -070023import org.onosproject.net.neighbour.NeighbourMessageContext;
Pier Ventreb6b81d52016-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 Ventreb6a7f342016-11-26 21:05:22 -080030import org.onosproject.net.host.HostService;
Pier Ventreb6b81d52016-12-02 08:16:05 -080031import org.onosproject.net.packet.DefaultOutboundPacket;
32import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
Pier Ventreb6a7f342016-11-26 21:05:22 -080033import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
35
Pier Ventreb6b81d52016-12-02 08:16:05 -080036import java.nio.ByteBuffer;
37
38import static com.google.common.base.Preconditions.checkNotNull;
39
Pier Ventreb6a7f342016-11-26 21:05:22 -080040/**
Pier Ventreb6b81d52016-12-02 08:16:05 -080041 * This handler provides provides useful functions to the
42 * neighbour handlers (ARP, NDP).
Pier Ventreb6a7f342016-11-26 21:05:22 -080043 */
Pier Ventreb6b81d52016-12-02 08:16:05 -080044public class SegmentRoutingNeighbourHandler {
Pier Ventreb6a7f342016-11-26 21:05:22 -080045
46 private static Logger log = LoggerFactory.getLogger(SegmentRoutingNeighbourHandler.class);
Pier Ventreb6b81d52016-12-02 08:16:05 -080047
48 protected SegmentRoutingManager srManager;
49 protected DeviceConfiguration config;
Pier Ventreb6a7f342016-11-26 21:05:22 -080050
51 /**
Pier Ventreb6b81d52016-12-02 08:16:05 -080052 * Creates an SegmentRoutingNeighbourHandler object.
Pier Ventreb6a7f342016-11-26 21:05:22 -080053 *
Pier Ventreb6b81d52016-12-02 08:16:05 -080054 * @param srManager SegmentRoutingManager object
Pier Ventreb6a7f342016-11-26 21:05:22 -080055 */
Pier Ventreb6b81d52016-12-02 08:16:05 -080056 public SegmentRoutingNeighbourHandler(SegmentRoutingManager srManager) {
57 this.srManager = srManager;
58 this.config = checkNotNull(srManager.deviceConfiguration);
Pier Ventreb6a7f342016-11-26 21:05:22 -080059 }
60
Pier Ventreb6b81d52016-12-02 08:16:05 -080061 /**
62 * Creates an SegmentRoutingNeighbourHandler object.
Pier Ventreb6a7f342016-11-26 21:05:22 -080063 */
Pier Ventreb6b81d52016-12-02 08:16:05 -080064 public SegmentRoutingNeighbourHandler() {
65 this.srManager = null;
66 this.config = null;
67 }
68
69 /**
70 * Retrieve router (device) info.
71 *
72 * @param mac where to copy the mac
73 * @param ip where to copy the ip
74 * @param deviceId the device id
75 * @param targetAddress the target address
Pier Luigi6a83c4a2017-01-29 12:38:48 -080076 * @return true if it was possible to get the necessary info.
77 * False for errors
Pier Ventreb6b81d52016-12-02 08:16:05 -080078 */
Pier Luigi6a83c4a2017-01-29 12:38:48 -080079 protected boolean getSenderInfo(byte[] mac,
Pier Ventreb6b81d52016-12-02 08:16:05 -080080 byte[] ip,
81 DeviceId deviceId,
82 IpAddress targetAddress) {
83 byte[] senderMacAddress;
84 byte[] senderIpAddress;
Pier Luigi6a83c4a2017-01-29 12:38:48 -080085 IpAddress sender;
Pier Ventreb6b81d52016-12-02 08:16:05 -080086 try {
87 senderMacAddress = config.getDeviceMac(deviceId).toBytes();
Pier Ventre6b2c1b32016-12-09 17:26:04 -080088 if (targetAddress.isIp4()) {
Pier Luigi6a83c4a2017-01-29 12:38:48 -080089 sender = config.getRouterIpAddressForASubnetHost(targetAddress.getIp4Address());
Pier Ventre6b2c1b32016-12-09 17:26:04 -080090 } else {
Pier Luigi6a83c4a2017-01-29 12:38:48 -080091 sender = config.getRouterIpAddressForASubnetHost(targetAddress.getIp6Address());
Pier Ventre6b2c1b32016-12-09 17:26:04 -080092 }
Pier Luigi6a83c4a2017-01-29 12:38:48 -080093 // If sender is null we abort.
94 if (sender == null) {
95 log.warn("Sender ip is null. Aborting getSenderInfo");
96 return false;
97 }
98 senderIpAddress = sender.toOctets();
Pier Ventreb6b81d52016-12-02 08:16:05 -080099 } catch (DeviceConfigNotFoundException e) {
Pier Luigi6a83c4a2017-01-29 12:38:48 -0800100 log.warn(e.getMessage() + " Aborting getSenderInfo");
101 return false;
Pier Ventreb6b81d52016-12-02 08:16:05 -0800102 }
Pier Ventreb6b81d52016-12-02 08:16:05 -0800103 System.arraycopy(senderMacAddress, 0, mac, 0, senderMacAddress.length);
104 System.arraycopy(senderIpAddress, 0, ip, 0, senderIpAddress.length);
Pier Luigi6a83c4a2017-01-29 12:38:48 -0800105 return true;
Pier Ventreb6b81d52016-12-02 08:16:05 -0800106 }
107
108 /**
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700109 * Reads the boolean configuration for responding to unknown hosts.
110 *
111 * @return respondToUnknownHosts boolean.
112 */
113 protected boolean respondToUnknownHosts() {
114 return srManager.respondToUnknownHosts;
115 }
116
117 /**
Pier Ventreb6b81d52016-12-02 08:16:05 -0800118 * Utility to send a ND reply using the supplied information.
119 *
120 * @param pkt the request
121 * @param targetMac the target mac
122 * @param hostService the host service
Charles Chan1e544ca2018-06-29 14:28:39 -0700123 * @param isRouter true if this reply is sent on behalf of a router
Pier Ventreb6b81d52016-12-02 08:16:05 -0800124 */
Charles Chan1e544ca2018-06-29 14:28:39 -0700125 protected void sendResponse(NeighbourMessageContext pkt, MacAddress targetMac, HostService hostService,
126 boolean isRouter) {
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700127 // if this is false, check if host exists in the store
128 if (!respondToUnknownHosts()) {
129 short vlanId = pkt.packet().getQinQVID();
130 HostId dstId = HostId.hostId(pkt.srcMac(), vlanId == Ethernet.VLAN_UNTAGGED
131 ? pkt.vlan() : VlanId.vlanId(vlanId));
132 Host dst = hostService.getHost(dstId);
133 if (dst == null) {
134 log.warn("Cannot send {} response to host {} - does not exist in the store",
135 pkt.protocol(), dstId);
136 return;
137 }
Pier Ventreb6b81d52016-12-02 08:16:05 -0800138 }
Charles Chan1e544ca2018-06-29 14:28:39 -0700139 pkt.setIsRouter(isRouter);
Pier Ventreb6b81d52016-12-02 08:16:05 -0800140 pkt.reply(targetMac);
141 }
142
143 /**
144 * Flood to all ports in the same subnet.
145 *
146 * @param packet packet to be flooded
147 * @param inPort where the packet comes from
148 * @param targetAddress the target address
149 */
150 protected void flood(Ethernet packet, ConnectPoint inPort, IpAddress targetAddress) {
151 try {
152 srManager.deviceConfiguration
153 .getSubnetPortsMap(inPort.deviceId()).forEach((subnet, ports) -> {
154 if (subnet.contains(targetAddress)) {
155 ports.stream()
156 .filter(port -> port != inPort.port())
157 .forEach(port -> {
158 forward(packet, new ConnectPoint(inPort.deviceId(), port));
159 });
Pier Ventreb6a7f342016-11-26 21:05:22 -0800160 }
Pier Ventreb6b81d52016-12-02 08:16:05 -0800161 });
162 } catch (DeviceConfigNotFoundException e) {
163 log.warn(e.getMessage()
164 + " Cannot flood in subnet as device config not available"
165 + " for device: " + inPort.deviceId());
Pier Ventreb6a7f342016-11-26 21:05:22 -0800166 }
167 }
168
Pier Ventreb6b81d52016-12-02 08:16:05 -0800169 /*
170 * Floods only on the port which have been configured with the subnet
171 * of the target address. The in port is excluded.
172 *
173 * @param pkt the ndp/arp packet and context information
174 */
175 protected void flood(NeighbourMessageContext pkt) {
176 try {
177 srManager.deviceConfiguration
178 .getSubnetPortsMap(pkt.inPort().deviceId()).forEach((subnet, ports) -> {
179 if (subnet.contains(pkt.target())) {
180 ports.stream()
181 .filter(port -> port != pkt.inPort().port())
182 .forEach(port -> {
183 ConnectPoint outPoint = new ConnectPoint(
184 pkt.inPort().deviceId(),
185 port
186 );
187 pkt.forward(outPoint);
188 });
189 }
190 });
191 } catch (DeviceConfigNotFoundException e) {
192 log.warn(e.getMessage()
193 + " Cannot flood in subnet as device config not available"
194 + " for device: " + pkt.inPort().deviceId());
195 }
196 }
197
198 /**
199 * Packet out to given port.
200 *
201 * Note: In current implementation, we expect all communication with
202 * end hosts within a subnet to be untagged.
203 * <p>
204 * For those pipelines that internally assigns a VLAN, the VLAN tag will be
205 * removed before egress.
206 * <p>
207 * For those pipelines that do not assign internal VLAN, the packet remains
208 * untagged.
209 *
210 * @param packet packet to be forwarded
211 * @param outPort where the packet should be forwarded
212 */
213 private void forward(Ethernet packet, ConnectPoint outPort) {
214 ByteBuffer buf = ByteBuffer.wrap(packet.serialize());
215
216 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
217 tbuilder.setOutput(outPort.port());
218 srManager.packetService.emit(new DefaultOutboundPacket(outPort.deviceId(),
219 tbuilder.build(), buf));
220 }
Pier Ventreb6a7f342016-11-26 21:05:22 -0800221
222}