-
Notifications
You must be signed in to change notification settings - Fork 0
/
NdpReplyComponent.java
296 lines (258 loc) · 11.1 KB
/
NdpReplyComponent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
* Copyright 2019-present Open Networking Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ngsdn.tutorial;
import org.onlab.packet.Ip6Address;
import org.onlab.packet.IpAddress;
import org.onlab.packet.MacAddress;
import org.onlab.util.ItemNotFoundException;
import org.onosproject.core.ApplicationId;
import org.onosproject.mastership.MastershipService;
import org.onosproject.net.DeviceId;
import org.onosproject.net.config.NetworkConfigService;
import org.onosproject.net.device.DeviceEvent;
import org.onosproject.net.device.DeviceListener;
import org.onosproject.net.device.DeviceService;
import org.onosproject.net.flow.FlowRule;
import org.onosproject.net.flow.FlowRuleOperations;
import org.onosproject.net.flow.FlowRuleService;
import org.onosproject.net.flow.criteria.PiCriterion;
import org.onosproject.net.host.InterfaceIpAddress;
import org.onosproject.net.intf.Interface;
import org.onosproject.net.intf.InterfaceService;
import org.onosproject.net.pi.model.PiActionId;
import org.onosproject.net.pi.model.PiActionParamId;
import org.onosproject.net.pi.model.PiMatchFieldId;
import org.onosproject.net.pi.runtime.PiAction;
import org.onosproject.net.pi.runtime.PiActionParam;
import org.onosproject.ngsdn.tutorial.common.FabricDeviceConfig;
import org.onosproject.ngsdn.tutorial.common.Utils;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collection;
import java.util.stream.Collectors;
import static org.onosproject.ngsdn.tutorial.AppConstants.INITIAL_SETUP_DELAY;
/**
* App component that configures devices to generate NDP Neighbor Advertisement
* packets for all interface IPv6 addresses configured in the netcfg.
*/
@Component(
immediate = true,
// *** TODO EXERCISE 5
// Enable component (enabled = true)
enabled = false
)
public class NdpReplyComponent {
private static final Logger log =
LoggerFactory.getLogger(NdpReplyComponent.class.getName());
//--------------------------------------------------------------------------
// ONOS CORE SERVICE BINDING
//
// These variables are set by the Karaf runtime environment before calling
// the activate() method.
//--------------------------------------------------------------------------
@Reference(cardinality = ReferenceCardinality.MANDATORY)
protected NetworkConfigService configService;
@Reference(cardinality = ReferenceCardinality.MANDATORY)
protected FlowRuleService flowRuleService;
@Reference(cardinality = ReferenceCardinality.MANDATORY)
protected InterfaceService interfaceService;
@Reference(cardinality = ReferenceCardinality.MANDATORY)
protected MastershipService mastershipService;
@Reference(cardinality = ReferenceCardinality.MANDATORY)
protected DeviceService deviceService;
@Reference(cardinality = ReferenceCardinality.MANDATORY)
private MainComponent mainComponent;
private DeviceListener deviceListener = new InternalDeviceListener();
private ApplicationId appId;
//--------------------------------------------------------------------------
// COMPONENT ACTIVATION.
//
// When loading/unloading the app the Karaf runtime environment will call
// activate()/deactivate().
//--------------------------------------------------------------------------
@Activate
public void activate() {
appId = mainComponent.getAppId();
// Register listeners to be informed about device events.
deviceService.addListener(deviceListener);
// Schedule set up of existing devices. Needed when reloading the app.
mainComponent.scheduleTask(this::setUpAllDevices, INITIAL_SETUP_DELAY);
log.info("Started");
}
@Deactivate
public void deactivate() {
deviceService.removeListener(deviceListener);
log.info("Stopped");
}
//--------------------------------------------------------------------------
// METHODS TO COMPLETE.
//
// Complete the implementation wherever you see TODO.
//--------------------------------------------------------------------------
/**
* Set up all devices for which this ONOS instance is currently master.
*/
private void setUpAllDevices() {
deviceService.getAvailableDevices().forEach(device -> {
if (mastershipService.isLocalMaster(device.id())) {
log.info("*** NDP REPLY - Starting Initial set up for {}...", device.id());
setUpDevice(device.id());
}
});
}
/**
* Performs setup of the given device by creating a flow rule to generate
* NDP NA packets for IPv6 addresses associated to the device interfaces.
*
* @param deviceId device ID
*/
private void setUpDevice(DeviceId deviceId) {
// Get this device config from netcfg.json.
final FabricDeviceConfig config = configService.getConfig(
deviceId, FabricDeviceConfig.class);
if (config == null) {
// Config not available yet
throw new ItemNotFoundException("Missing fabricDeviceConfig for " + deviceId);
}
// Get this device myStation mac.
final MacAddress deviceMac = config.myStationMac();
// Get all interfaces currently configured for the device
final Collection<Interface> interfaces = interfaceService.getInterfaces()
.stream()
.filter(iface -> iface.connectPoint().deviceId().equals(deviceId))
.collect(Collectors.toSet());
if (interfaces.isEmpty()) {
log.info("{} does not have any IPv6 interface configured",
deviceId);
return;
}
// Generate and install flow rules.
log.info("Adding rules to {} to generate NDP NA for {} IPv6 interfaces...",
deviceId, interfaces.size());
final Collection<FlowRule> flowRules = interfaces.stream()
.map(this::getIp6Addresses)
.flatMap(Collection::stream)
.map(ipv6addr -> buildNdpReplyFlowRule(deviceId, ipv6addr, deviceMac))
.collect(Collectors.toSet());
installRules(flowRules);
}
/**
* Build a flow rule for the NDP reply table on the given device, for the
* given target IPv6 address and MAC address.
*
* @param deviceId device ID where to install the flow rules
* @param targetIpv6Address target IPv6 address
* @param targetMac target MAC address
* @return flow rule object
*/
private FlowRule buildNdpReplyFlowRule(DeviceId deviceId,
Ip6Address targetIpv6Address,
MacAddress targetMac) {
// *** TODO EXERCISE 5
// Modify P4Runtime entity names to match content of P4Info file (look
// for the fully qualified name of tables, match fields, and actions.
// ---- START SOLUTION ----
// Build match.
final PiCriterion match = PiCriterion.builder()
.matchExact(PiMatchFieldId.of("hdr.ndp.target_ipv6_addr"), targetIpv6Address.toOctets())
.build();
// Build action.
final PiActionParam targetMacParam = new PiActionParam(
PiActionParamId.of("target_mac"), targetMac.toBytes());
final PiAction action = PiAction.builder()
.withId(PiActionId.of("MODIFY ME"))
.withParameter(targetMacParam)
.build();
// Table ID.
final String tableId = "MODIFY ME";
// ---- END SOLUTION ----
// Build flow rule.
final FlowRule rule = Utils.buildFlowRule(
deviceId, appId, tableId, match, action);
return rule;
}
//--------------------------------------------------------------------------
// EVENT LISTENERS
//
// Events are processed only if isRelevant() returns true.
//--------------------------------------------------------------------------
/**
* Listener of device events.
*/
public class InternalDeviceListener implements DeviceListener {
@Override
public boolean isRelevant(DeviceEvent event) {
switch (event.type()) {
case DEVICE_ADDED:
case DEVICE_AVAILABILITY_CHANGED:
break;
default:
// Ignore other events.
return false;
}
// Process only if this controller instance is the master.
final DeviceId deviceId = event.subject().id();
return mastershipService.isLocalMaster(deviceId);
}
@Override
public void event(DeviceEvent event) {
final DeviceId deviceId = event.subject().id();
if (deviceService.isAvailable(deviceId)) {
// A P4Runtime device is considered available in ONOS when there
// is a StreamChannel session open and the pipeline
// configuration has been set.
// Events are processed using a thread pool defined in the
// MainComponent.
mainComponent.getExecutorService().execute(() -> {
log.info("{} event! deviceId={}", event.type(), deviceId);
setUpDevice(deviceId);
});
}
}
}
//--------------------------------------------------------------------------
// UTILITY METHODS
//--------------------------------------------------------------------------
/**
* Returns all IPv6 addresses associated with the given interface.
*
* @param iface interface instance
* @return collection of IPv6 addresses
*/
private Collection<Ip6Address> getIp6Addresses(Interface iface) {
return iface.ipAddressesList()
.stream()
.map(InterfaceIpAddress::ipAddress)
.filter(IpAddress::isIp6)
.map(IpAddress::getIp6Address)
.collect(Collectors.toSet());
}
/**
* Install the given flow rules in batch using the flow rule service.
*
* @param flowRules flow rules to install
*/
private void installRules(Collection<FlowRule> flowRules) {
FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
flowRules.forEach(ops::add);
flowRuleService.apply(ops.build());
}
}