Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 1 | /* |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.drivers.bmv2; |
| 18 | |
Carmelo Cascone | d4e7a77 | 2016-05-03 11:21:29 -0700 | [diff] [blame] | 19 | import com.eclipsesource.json.Json; |
| 20 | import com.google.common.cache.CacheBuilder; |
| 21 | import com.google.common.cache.CacheLoader; |
| 22 | import com.google.common.cache.LoadingCache; |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 23 | import com.google.common.collect.Lists; |
| 24 | import com.google.common.collect.Maps; |
Carmelo Cascone | a2f510e | 2016-05-03 18:36:45 -0700 | [diff] [blame] | 25 | import com.google.common.collect.Sets; |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 26 | import org.apache.commons.lang3.tuple.Pair; |
| 27 | import org.apache.commons.lang3.tuple.Triple; |
Carmelo Cascone | d4e7a77 | 2016-05-03 11:21:29 -0700 | [diff] [blame] | 28 | import org.onosproject.bmv2.api.model.Bmv2Model; |
Carmelo Cascone | 37d5dbf | 2016-04-18 15:15:48 -0700 | [diff] [blame] | 29 | import org.onosproject.bmv2.api.runtime.Bmv2Client; |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 30 | import org.onosproject.bmv2.api.runtime.Bmv2MatchKey; |
Carmelo Cascone | aa8b629 | 2016-04-13 14:27:06 -0700 | [diff] [blame] | 31 | import org.onosproject.bmv2.api.runtime.Bmv2RuntimeException; |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 32 | import org.onosproject.bmv2.api.runtime.Bmv2TableEntry; |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 33 | import org.onosproject.bmv2.ctl.Bmv2ThriftClient; |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 34 | import org.onosproject.drivers.bmv2.translators.Bmv2DefaultFlowRuleTranslator; |
| 35 | import org.onosproject.drivers.bmv2.translators.Bmv2FlowRuleTranslator; |
| 36 | import org.onosproject.drivers.bmv2.translators.Bmv2FlowRuleTranslatorException; |
Carmelo Cascone | d4e7a77 | 2016-05-03 11:21:29 -0700 | [diff] [blame] | 37 | import org.onosproject.drivers.bmv2.translators.Bmv2SimpleTranslatorConfig; |
| 38 | import org.onosproject.net.Device; |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 39 | import org.onosproject.net.DeviceId; |
Carmelo Cascone | d4e7a77 | 2016-05-03 11:21:29 -0700 | [diff] [blame] | 40 | import org.onosproject.net.device.DeviceService; |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 41 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 42 | import org.onosproject.net.flow.DefaultFlowEntry; |
| 43 | import org.onosproject.net.flow.FlowEntry; |
| 44 | import org.onosproject.net.flow.FlowRule; |
| 45 | import org.onosproject.net.flow.FlowRuleProgrammable; |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 46 | import org.slf4j.Logger; |
| 47 | import org.slf4j.LoggerFactory; |
| 48 | |
| 49 | import java.util.Collection; |
| 50 | import java.util.Collections; |
Carmelo Cascone | f8cf288 | 2016-05-04 14:06:17 -0700 | [diff] [blame^] | 51 | import java.util.Date; |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 52 | import java.util.List; |
Carmelo Cascone | a2f510e | 2016-05-03 18:36:45 -0700 | [diff] [blame] | 53 | import java.util.Set; |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 54 | import java.util.concurrent.ConcurrentMap; |
Carmelo Cascone | d4e7a77 | 2016-05-03 11:21:29 -0700 | [diff] [blame] | 55 | import java.util.concurrent.ExecutionException; |
| 56 | import java.util.concurrent.TimeUnit; |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 57 | |
Carmelo Cascone | f8cf288 | 2016-05-04 14:06:17 -0700 | [diff] [blame^] | 58 | import static org.onosproject.net.flow.FlowEntry.FlowEntryState.ADDED; |
| 59 | |
Carmelo Cascone | e912164 | 2016-04-27 17:02:38 -0700 | [diff] [blame] | 60 | /** |
| 61 | * Flow rule programmable device behaviour implementation for BMv2. |
| 62 | */ |
| 63 | public class Bmv2FlowRuleProgrammable extends AbstractHandlerBehaviour |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 64 | implements FlowRuleProgrammable { |
| 65 | |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 66 | private static final Logger LOG = |
Carmelo Cascone | e912164 | 2016-04-27 17:02:38 -0700 | [diff] [blame] | 67 | LoggerFactory.getLogger(Bmv2FlowRuleProgrammable.class); |
Carmelo Cascone | d4e7a77 | 2016-05-03 11:21:29 -0700 | [diff] [blame] | 68 | |
Carmelo Cascone | a2f510e | 2016-05-03 18:36:45 -0700 | [diff] [blame] | 69 | // There's no Bmv2 client method to poll flow entries from the device. Use a local store. |
| 70 | // FIXME: this information should be distributed across instances of the cluster. |
Carmelo Cascone | f8cf288 | 2016-05-04 14:06:17 -0700 | [diff] [blame^] | 71 | private static final ConcurrentMap<Triple<DeviceId, String, Bmv2MatchKey>, Pair<Long, TimestampedFlowRule>> |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 72 | ENTRIES_MAP = Maps.newConcurrentMap(); |
Carmelo Cascone | d4e7a77 | 2016-05-03 11:21:29 -0700 | [diff] [blame] | 73 | |
| 74 | // Cache model objects instead of parsing the JSON each time. |
| 75 | private static final LoadingCache<String, Bmv2Model> MODEL_CACHE = CacheBuilder.newBuilder() |
| 76 | .expireAfterAccess(60, TimeUnit.SECONDS) |
| 77 | .build(new CacheLoader<String, Bmv2Model>() { |
| 78 | @Override |
| 79 | public Bmv2Model load(String jsonString) throws Exception { |
| 80 | // Expensive call. |
| 81 | return Bmv2Model.parse(Json.parse(jsonString).asObject()); |
| 82 | } |
| 83 | }); |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 84 | |
| 85 | @Override |
| 86 | public Collection<FlowEntry> getFlowEntries() { |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 87 | |
| 88 | DeviceId deviceId = handler().data().deviceId(); |
| 89 | |
Carmelo Cascone | a2f510e | 2016-05-03 18:36:45 -0700 | [diff] [blame] | 90 | Bmv2Client deviceClient; |
| 91 | try { |
| 92 | deviceClient = Bmv2ThriftClient.of(deviceId); |
| 93 | } catch (Bmv2RuntimeException e) { |
| 94 | LOG.error("Failed to connect to Bmv2 device", e); |
| 95 | return Collections.emptyList(); |
| 96 | } |
| 97 | |
| 98 | Bmv2Model model = getTranslator(deviceId).config().model(); |
| 99 | |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 100 | List<FlowEntry> entryList = Lists.newArrayList(); |
| 101 | |
Carmelo Cascone | a2f510e | 2016-05-03 18:36:45 -0700 | [diff] [blame] | 102 | model.tables().forEach(table -> { |
| 103 | // For each table declared in the model for this device, do: |
| 104 | try { |
| 105 | // Bmv2 doesn't support proper polling for table entries, but only a string based table dump. |
| 106 | // The trick here is to first dump the entry ids currently installed in the device for a given table, |
| 107 | // and then filter ENTRIES_MAP based on the retrieved values. |
| 108 | Set<Long> installedEntryIds = Sets.newHashSet(deviceClient.getInstalledEntryIds(table.name())); |
| 109 | ENTRIES_MAP.forEach((key, value) -> { |
| 110 | if (key.getLeft() == deviceId && key.getMiddle() == table.name() |
| 111 | && value != null) { |
Carmelo Cascone | f8cf288 | 2016-05-04 14:06:17 -0700 | [diff] [blame^] | 112 | long entryId = value.getKey(); |
Carmelo Cascone | a2f510e | 2016-05-03 18:36:45 -0700 | [diff] [blame] | 113 | // Filter entries_map for this device and table. |
Carmelo Cascone | f8cf288 | 2016-05-04 14:06:17 -0700 | [diff] [blame^] | 114 | if (installedEntryIds.contains(entryId)) { |
Carmelo Cascone | a2f510e | 2016-05-03 18:36:45 -0700 | [diff] [blame] | 115 | // Entry is installed. |
Carmelo Cascone | f8cf288 | 2016-05-04 14:06:17 -0700 | [diff] [blame^] | 116 | long bytes = 0L; |
| 117 | long packets = 0L; |
| 118 | if (table.hasCounters()) { |
| 119 | // Read counter values from device. |
| 120 | try { |
| 121 | Pair<Long, Long> counterValue = deviceClient.readTableEntryCounter(table.name(), |
| 122 | entryId); |
| 123 | bytes = counterValue.getLeft(); |
| 124 | packets = counterValue.getRight(); |
| 125 | } catch (Bmv2RuntimeException e) { |
| 126 | LOG.warn("Unable to get counter values for entry {} of table {} of device {}: {}", |
| 127 | entryId, table.name(), deviceId, e.toString()); |
| 128 | } |
| 129 | } |
| 130 | TimestampedFlowRule tsRule = value.getRight(); |
| 131 | FlowEntry entry = new DefaultFlowEntry(tsRule.rule(), ADDED, |
| 132 | tsRule.lifeInSeconds(), packets, bytes); |
| 133 | entryList.add(entry); |
Carmelo Cascone | a2f510e | 2016-05-03 18:36:45 -0700 | [diff] [blame] | 134 | } else { |
| 135 | // No such entry on device, can remove from local store. |
| 136 | ENTRIES_MAP.remove(key); |
| 137 | } |
| 138 | } |
| 139 | }); |
| 140 | } catch (Bmv2RuntimeException e) { |
| 141 | LOG.error("Unable to get flow entries for table {} of device {}: {}", |
| 142 | table.name(), deviceId, e.toString()); |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 143 | } |
| 144 | }); |
| 145 | |
| 146 | return Collections.unmodifiableCollection(entryList); |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public Collection<FlowRule> applyFlowRules(Collection<FlowRule> rules) { |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 151 | |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 152 | return processFlowRules(rules, Operation.APPLY); |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public Collection<FlowRule> removeFlowRules(Collection<FlowRule> rules) { |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 157 | |
| 158 | return processFlowRules(rules, Operation.REMOVE); |
| 159 | } |
| 160 | |
| 161 | private Collection<FlowRule> processFlowRules(Collection<FlowRule> rules, Operation operation) { |
| 162 | |
| 163 | DeviceId deviceId = handler().data().deviceId(); |
| 164 | |
Carmelo Cascone | 37d5dbf | 2016-04-18 15:15:48 -0700 | [diff] [blame] | 165 | Bmv2Client deviceClient; |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 166 | try { |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 167 | deviceClient = Bmv2ThriftClient.of(deviceId); |
Carmelo Cascone | aa8b629 | 2016-04-13 14:27:06 -0700 | [diff] [blame] | 168 | } catch (Bmv2RuntimeException e) { |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 169 | LOG.error("Failed to connect to Bmv2 device", e); |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 170 | return Collections.emptyList(); |
| 171 | } |
| 172 | |
Carmelo Cascone | d4e7a77 | 2016-05-03 11:21:29 -0700 | [diff] [blame] | 173 | Bmv2FlowRuleTranslator translator = getTranslator(deviceId); |
| 174 | |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 175 | List<FlowRule> processedFlowRules = Lists.newArrayList(); |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 176 | |
| 177 | for (FlowRule rule : rules) { |
| 178 | |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 179 | Bmv2TableEntry bmv2Entry; |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 180 | |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 181 | try { |
Carmelo Cascone | d4e7a77 | 2016-05-03 11:21:29 -0700 | [diff] [blame] | 182 | bmv2Entry = translator.translate(rule); |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 183 | } catch (Bmv2FlowRuleTranslatorException e) { |
| 184 | LOG.error("Unable to translate flow rule: {}", e.getMessage()); |
| 185 | continue; |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 186 | } |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 187 | |
| 188 | String tableName = bmv2Entry.tableName(); |
| 189 | Triple<DeviceId, String, Bmv2MatchKey> entryKey = Triple.of(deviceId, tableName, bmv2Entry.matchKey()); |
| 190 | |
| 191 | /* |
| 192 | From here on threads are synchronized over entryKey, i.e. serialize operations |
| 193 | over the same matchKey of a specific table and device. |
| 194 | */ |
| 195 | ENTRIES_MAP.compute(entryKey, (key, value) -> { |
| 196 | try { |
| 197 | if (operation == Operation.APPLY) { |
| 198 | // Apply entry |
| 199 | long entryId; |
Carmelo Cascone | a2f510e | 2016-05-03 18:36:45 -0700 | [diff] [blame] | 200 | if (value != null) { |
| 201 | // Existing entry. |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 202 | entryId = value.getKey(); |
Carmelo Cascone | a2f510e | 2016-05-03 18:36:45 -0700 | [diff] [blame] | 203 | try { |
| 204 | // Tentatively delete entry before re-adding. |
| 205 | // It might not exist on device due to inconsistencies. |
| 206 | deviceClient.deleteTableEntry(bmv2Entry.tableName(), entryId); |
Carmelo Cascone | f8cf288 | 2016-05-04 14:06:17 -0700 | [diff] [blame^] | 207 | value = null; |
Carmelo Cascone | a2f510e | 2016-05-03 18:36:45 -0700 | [diff] [blame] | 208 | } catch (Bmv2RuntimeException e) { |
| 209 | // Silently drop exception as we can probably fix this by re-adding the entry. |
| 210 | } |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 211 | } |
Carmelo Cascone | a2f510e | 2016-05-03 18:36:45 -0700 | [diff] [blame] | 212 | // Add entry. |
| 213 | entryId = deviceClient.addTableEntry(bmv2Entry); |
Carmelo Cascone | f8cf288 | 2016-05-04 14:06:17 -0700 | [diff] [blame^] | 214 | value = Pair.of(entryId, new TimestampedFlowRule(rule)); |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 215 | } else { |
| 216 | // Remove entry |
| 217 | if (value == null) { |
| 218 | // Entry not found in map, how come? |
| 219 | LOG.debug("Trying to remove entry, but entry ID not found: " + entryKey); |
| 220 | } else { |
| 221 | deviceClient.deleteTableEntry(tableName, value.getKey()); |
| 222 | value = null; |
| 223 | } |
| 224 | } |
| 225 | // If here, no exceptions... things went well :) |
| 226 | processedFlowRules.add(rule); |
| 227 | } catch (Bmv2RuntimeException e) { |
Carmelo Cascone | a2f510e | 2016-05-03 18:36:45 -0700 | [diff] [blame] | 228 | LOG.warn("Unable to {} flow rule: {}", operation.name().toLowerCase(), e.toString()); |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 229 | } |
| 230 | return value; |
| 231 | }); |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 234 | return processedFlowRules; |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Carmelo Cascone | d4e7a77 | 2016-05-03 11:21:29 -0700 | [diff] [blame] | 237 | /** |
| 238 | * Gets the appropriate flow rule translator based on the device running configuration. |
| 239 | * |
| 240 | * @param deviceId a device id |
| 241 | * @return a flow rule translator |
| 242 | */ |
| 243 | private Bmv2FlowRuleTranslator getTranslator(DeviceId deviceId) { |
| 244 | |
| 245 | DeviceService deviceService = handler().get(DeviceService.class); |
| 246 | if (deviceService == null) { |
| 247 | LOG.error("Unable to get device service"); |
| 248 | return null; |
| 249 | } |
| 250 | |
| 251 | Device device = deviceService.getDevice(deviceId); |
| 252 | if (device == null) { |
| 253 | LOG.error("Unable to get device {}", deviceId); |
| 254 | return null; |
| 255 | } |
| 256 | |
| 257 | String jsonString = device.annotations().value("bmv2JsonConfigValue"); |
| 258 | if (jsonString == null) { |
| 259 | LOG.error("Unable to read bmv2 JSON config from device {}", deviceId); |
| 260 | return null; |
| 261 | } |
| 262 | |
| 263 | Bmv2Model model; |
| 264 | try { |
| 265 | model = MODEL_CACHE.get(jsonString); |
| 266 | } catch (ExecutionException e) { |
| 267 | LOG.error("Unable to parse bmv2 JSON config for device {}:", deviceId, e.getCause()); |
| 268 | return null; |
| 269 | } |
| 270 | |
| 271 | // TODO: get translator config dynamically. |
| 272 | // Now it's hardcoded, selection should be based on the device bmv2 model. |
| 273 | Bmv2FlowRuleTranslator.TranslatorConfig translatorConfig = new Bmv2SimpleTranslatorConfig(model); |
| 274 | return new Bmv2DefaultFlowRuleTranslator(translatorConfig); |
| 275 | } |
| 276 | |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 277 | private enum Operation { |
| 278 | APPLY, REMOVE |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 279 | } |
Carmelo Cascone | f8cf288 | 2016-05-04 14:06:17 -0700 | [diff] [blame^] | 280 | |
| 281 | private class TimestampedFlowRule { |
| 282 | private final FlowRule rule; |
| 283 | private final Date addedDate; |
| 284 | |
| 285 | public TimestampedFlowRule(FlowRule rule) { |
| 286 | this.rule = rule; |
| 287 | this.addedDate = new Date(); |
| 288 | } |
| 289 | |
| 290 | public FlowRule rule() { |
| 291 | return rule; |
| 292 | } |
| 293 | |
| 294 | public long lifeInSeconds() { |
| 295 | return (new Date().getTime() - addedDate.getTime()) / 1000; |
| 296 | } |
| 297 | } |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 298 | } |