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 | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 25 | import org.apache.commons.lang3.tuple.Pair; |
| 26 | import org.apache.commons.lang3.tuple.Triple; |
Carmelo Cascone | d4e7a77 | 2016-05-03 11:21:29 -0700 | [diff] [blame^] | 27 | import org.onosproject.bmv2.api.model.Bmv2Model; |
Carmelo Cascone | 37d5dbf | 2016-04-18 15:15:48 -0700 | [diff] [blame] | 28 | import org.onosproject.bmv2.api.runtime.Bmv2Client; |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 29 | import org.onosproject.bmv2.api.runtime.Bmv2MatchKey; |
Carmelo Cascone | aa8b629 | 2016-04-13 14:27:06 -0700 | [diff] [blame] | 30 | import org.onosproject.bmv2.api.runtime.Bmv2RuntimeException; |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 31 | import org.onosproject.bmv2.api.runtime.Bmv2TableEntry; |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 32 | import org.onosproject.bmv2.ctl.Bmv2ThriftClient; |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 33 | import org.onosproject.drivers.bmv2.translators.Bmv2DefaultFlowRuleTranslator; |
| 34 | import org.onosproject.drivers.bmv2.translators.Bmv2FlowRuleTranslator; |
| 35 | import org.onosproject.drivers.bmv2.translators.Bmv2FlowRuleTranslatorException; |
Carmelo Cascone | d4e7a77 | 2016-05-03 11:21:29 -0700 | [diff] [blame^] | 36 | import org.onosproject.drivers.bmv2.translators.Bmv2SimpleTranslatorConfig; |
| 37 | import org.onosproject.net.Device; |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 38 | import org.onosproject.net.DeviceId; |
Carmelo Cascone | d4e7a77 | 2016-05-03 11:21:29 -0700 | [diff] [blame^] | 39 | import org.onosproject.net.device.DeviceService; |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 40 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 41 | import org.onosproject.net.flow.DefaultFlowEntry; |
| 42 | import org.onosproject.net.flow.FlowEntry; |
| 43 | import org.onosproject.net.flow.FlowRule; |
| 44 | import org.onosproject.net.flow.FlowRuleProgrammable; |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 45 | import org.slf4j.Logger; |
| 46 | import org.slf4j.LoggerFactory; |
| 47 | |
| 48 | import java.util.Collection; |
| 49 | import java.util.Collections; |
| 50 | import java.util.List; |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 51 | import java.util.concurrent.ConcurrentMap; |
Carmelo Cascone | d4e7a77 | 2016-05-03 11:21:29 -0700 | [diff] [blame^] | 52 | import java.util.concurrent.ExecutionException; |
| 53 | import java.util.concurrent.TimeUnit; |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 54 | |
Carmelo Cascone | e912164 | 2016-04-27 17:02:38 -0700 | [diff] [blame] | 55 | /** |
| 56 | * Flow rule programmable device behaviour implementation for BMv2. |
| 57 | */ |
| 58 | public class Bmv2FlowRuleProgrammable extends AbstractHandlerBehaviour |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 59 | implements FlowRuleProgrammable { |
| 60 | |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 61 | private static final Logger LOG = |
Carmelo Cascone | e912164 | 2016-04-27 17:02:38 -0700 | [diff] [blame] | 62 | LoggerFactory.getLogger(Bmv2FlowRuleProgrammable.class); |
Carmelo Cascone | d4e7a77 | 2016-05-03 11:21:29 -0700 | [diff] [blame^] | 63 | |
| 64 | // There's no Bmv2 client method to poll flow entries from the device device. Need a local store. |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 65 | private static final ConcurrentMap<Triple<DeviceId, String, Bmv2MatchKey>, Pair<Long, FlowEntry>> |
| 66 | ENTRIES_MAP = Maps.newConcurrentMap(); |
Carmelo Cascone | d4e7a77 | 2016-05-03 11:21:29 -0700 | [diff] [blame^] | 67 | |
| 68 | // Cache model objects instead of parsing the JSON each time. |
| 69 | private static final LoadingCache<String, Bmv2Model> MODEL_CACHE = CacheBuilder.newBuilder() |
| 70 | .expireAfterAccess(60, TimeUnit.SECONDS) |
| 71 | .build(new CacheLoader<String, Bmv2Model>() { |
| 72 | @Override |
| 73 | public Bmv2Model load(String jsonString) throws Exception { |
| 74 | // Expensive call. |
| 75 | return Bmv2Model.parse(Json.parse(jsonString).asObject()); |
| 76 | } |
| 77 | }); |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 78 | |
| 79 | @Override |
| 80 | public Collection<FlowEntry> getFlowEntries() { |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 81 | |
| 82 | DeviceId deviceId = handler().data().deviceId(); |
| 83 | |
| 84 | List<FlowEntry> entryList = Lists.newArrayList(); |
| 85 | |
| 86 | // FIXME: improve this, e.g. might store a separate Map<DeviceId, Collection<FlowEntry>> |
| 87 | ENTRIES_MAP.forEach((key, value) -> { |
| 88 | if (key.getLeft() == deviceId && value != null) { |
| 89 | entryList.add(value.getRight()); |
| 90 | } |
| 91 | }); |
| 92 | |
| 93 | return Collections.unmodifiableCollection(entryList); |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public Collection<FlowRule> applyFlowRules(Collection<FlowRule> rules) { |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 98 | |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 99 | return processFlowRules(rules, Operation.APPLY); |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public Collection<FlowRule> removeFlowRules(Collection<FlowRule> rules) { |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 104 | |
| 105 | return processFlowRules(rules, Operation.REMOVE); |
| 106 | } |
| 107 | |
| 108 | private Collection<FlowRule> processFlowRules(Collection<FlowRule> rules, Operation operation) { |
| 109 | |
| 110 | DeviceId deviceId = handler().data().deviceId(); |
| 111 | |
Carmelo Cascone | 37d5dbf | 2016-04-18 15:15:48 -0700 | [diff] [blame] | 112 | Bmv2Client deviceClient; |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 113 | try { |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 114 | deviceClient = Bmv2ThriftClient.of(deviceId); |
Carmelo Cascone | aa8b629 | 2016-04-13 14:27:06 -0700 | [diff] [blame] | 115 | } catch (Bmv2RuntimeException e) { |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 116 | LOG.error("Failed to connect to Bmv2 device", e); |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 117 | return Collections.emptyList(); |
| 118 | } |
| 119 | |
Carmelo Cascone | d4e7a77 | 2016-05-03 11:21:29 -0700 | [diff] [blame^] | 120 | Bmv2FlowRuleTranslator translator = getTranslator(deviceId); |
| 121 | |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 122 | List<FlowRule> processedFlowRules = Lists.newArrayList(); |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 123 | |
| 124 | for (FlowRule rule : rules) { |
| 125 | |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 126 | Bmv2TableEntry bmv2Entry; |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 127 | |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 128 | try { |
Carmelo Cascone | d4e7a77 | 2016-05-03 11:21:29 -0700 | [diff] [blame^] | 129 | bmv2Entry = translator.translate(rule); |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 130 | } catch (Bmv2FlowRuleTranslatorException e) { |
| 131 | LOG.error("Unable to translate flow rule: {}", e.getMessage()); |
| 132 | continue; |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 133 | } |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 134 | |
| 135 | String tableName = bmv2Entry.tableName(); |
| 136 | Triple<DeviceId, String, Bmv2MatchKey> entryKey = Triple.of(deviceId, tableName, bmv2Entry.matchKey()); |
| 137 | |
| 138 | /* |
| 139 | From here on threads are synchronized over entryKey, i.e. serialize operations |
| 140 | over the same matchKey of a specific table and device. |
| 141 | */ |
| 142 | ENTRIES_MAP.compute(entryKey, (key, value) -> { |
| 143 | try { |
| 144 | if (operation == Operation.APPLY) { |
| 145 | // Apply entry |
| 146 | long entryId; |
| 147 | if (value == null) { |
| 148 | // New entry |
| 149 | entryId = deviceClient.addTableEntry(bmv2Entry); |
| 150 | } else { |
| 151 | // Existing entry |
| 152 | entryId = value.getKey(); |
| 153 | // FIXME: check if priority or timeout changed |
| 154 | // In this case we should to re-add the entry (not modify) |
| 155 | deviceClient.modifyTableEntry(tableName, entryId, bmv2Entry.action()); |
| 156 | } |
| 157 | // TODO: evaluate flow entry life, bytes and packets |
| 158 | FlowEntry flowEntry = new DefaultFlowEntry( |
| 159 | rule, FlowEntry.FlowEntryState.ADDED, 0, 0, 0); |
| 160 | value = Pair.of(entryId, flowEntry); |
| 161 | } else { |
| 162 | // Remove entry |
| 163 | if (value == null) { |
| 164 | // Entry not found in map, how come? |
| 165 | LOG.debug("Trying to remove entry, but entry ID not found: " + entryKey); |
| 166 | } else { |
| 167 | deviceClient.deleteTableEntry(tableName, value.getKey()); |
| 168 | value = null; |
| 169 | } |
| 170 | } |
| 171 | // If here, no exceptions... things went well :) |
| 172 | processedFlowRules.add(rule); |
| 173 | } catch (Bmv2RuntimeException e) { |
| 174 | LOG.error("Unable to " + operation.name().toLowerCase() + " flow rule", e); |
| 175 | } catch (Exception e) { |
| 176 | LOG.error("Uncaught exception while processing flow rule", e); |
| 177 | } |
| 178 | return value; |
| 179 | }); |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 182 | return processedFlowRules; |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Carmelo Cascone | d4e7a77 | 2016-05-03 11:21:29 -0700 | [diff] [blame^] | 185 | /** |
| 186 | * Gets the appropriate flow rule translator based on the device running configuration. |
| 187 | * |
| 188 | * @param deviceId a device id |
| 189 | * @return a flow rule translator |
| 190 | */ |
| 191 | private Bmv2FlowRuleTranslator getTranslator(DeviceId deviceId) { |
| 192 | |
| 193 | DeviceService deviceService = handler().get(DeviceService.class); |
| 194 | if (deviceService == null) { |
| 195 | LOG.error("Unable to get device service"); |
| 196 | return null; |
| 197 | } |
| 198 | |
| 199 | Device device = deviceService.getDevice(deviceId); |
| 200 | if (device == null) { |
| 201 | LOG.error("Unable to get device {}", deviceId); |
| 202 | return null; |
| 203 | } |
| 204 | |
| 205 | String jsonString = device.annotations().value("bmv2JsonConfigValue"); |
| 206 | if (jsonString == null) { |
| 207 | LOG.error("Unable to read bmv2 JSON config from device {}", deviceId); |
| 208 | return null; |
| 209 | } |
| 210 | |
| 211 | Bmv2Model model; |
| 212 | try { |
| 213 | model = MODEL_CACHE.get(jsonString); |
| 214 | } catch (ExecutionException e) { |
| 215 | LOG.error("Unable to parse bmv2 JSON config for device {}:", deviceId, e.getCause()); |
| 216 | return null; |
| 217 | } |
| 218 | |
| 219 | // TODO: get translator config dynamically. |
| 220 | // Now it's hardcoded, selection should be based on the device bmv2 model. |
| 221 | Bmv2FlowRuleTranslator.TranslatorConfig translatorConfig = new Bmv2SimpleTranslatorConfig(model); |
| 222 | return new Bmv2DefaultFlowRuleTranslator(translatorConfig); |
| 223 | } |
| 224 | |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 225 | private enum Operation { |
| 226 | APPLY, REMOVE |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 227 | } |
Carmelo Cascone | 2954f13 | 2016-04-15 10:26:40 -0700 | [diff] [blame] | 228 | } |