Search in sources :

Example 1 with NicFlowRule

use of org.onosproject.drivers.server.devices.nic.NicFlowRule in project onos by opennetworkinglab.

the class FlowRuleProgrammableServerImpl method groupRules.

/**
 * Groups a set of FlowRules by their traffic class ID.
 *
 * @param rules set of NIC rules to install
 * @return a map of traffic class IDs to their set of NIC rules
 */
private Map<String, Set<FlowRule>> groupRules(Collection<FlowRule> rules) {
    Map<String, Set<FlowRule>> rulesPerTc = new ConcurrentHashMap<String, Set<FlowRule>>();
    rules.forEach(rule -> {
        if (!(rule instanceof FlowEntry)) {
            NicFlowRule nicRule = null;
            // Only NicFlowRules are accepted
            try {
                nicRule = (NicFlowRule) rule;
            } catch (ClassCastException cEx) {
                log.warn("Skipping flow rule not crafted for NIC: {}", rule);
            }
            if (nicRule != null) {
                String tcId = nicRule.trafficClassId();
                // Create a bucket of flow rules for this traffic class
                if (!rulesPerTc.containsKey(tcId)) {
                    rulesPerTc.put(tcId, Sets.<FlowRule>newConcurrentHashSet());
                }
                Set<FlowRule> tcRuleSet = rulesPerTc.get(tcId);
                tcRuleSet.add(nicRule);
            }
        }
    });
    return rulesPerTc;
}
Also used : Set(java.util.Set) NicFlowRule(org.onosproject.drivers.server.devices.nic.NicFlowRule) NicFlowRule(org.onosproject.drivers.server.devices.nic.NicFlowRule) FlowRule(org.onosproject.net.flow.FlowRule) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) FlowEntry(org.onosproject.net.flow.FlowEntry) DefaultFlowEntry(org.onosproject.net.flow.DefaultFlowEntry)

Example 2 with NicFlowRule

use of org.onosproject.drivers.server.devices.nic.NicFlowRule in project onos by opennetworkinglab.

the class FlowRuleProgrammableServerImpl method installNicFlowRules.

/**
 * Installs a set of FlowRules of the same traffic class ID
 * on a server device.
 *
 * @param deviceId target server device ID
 * @param trafficClassId traffic class ID of the NIC rules
 * @param rules set of NIC rules to install
 * @return a set of successfully installed NIC rules
 */
private Collection<FlowRule> installNicFlowRules(DeviceId deviceId, String trafficClassId, Collection<FlowRule> rules) {
    int rulesToInstall = rules.size();
    if (rulesToInstall == 0) {
        return Collections.EMPTY_LIST;
    }
    ObjectMapper mapper = new ObjectMapper();
    // Create the object node to host the list of rules
    ObjectNode scsObjNode = mapper.createObjectNode();
    // Add the service chain's traffic class ID that requested these rules
    scsObjNode.put(PARAM_ID, trafficClassId);
    // Create the object node to host the Rx filter method
    ObjectNode methodObjNode = mapper.createObjectNode();
    methodObjNode.put(PARAM_NIC_RX_METHOD, PARAM_NIC_RX_FILTER_FD);
    scsObjNode.put(PARAM_NIC_RX_FILTER, methodObjNode);
    // Map each core to an array of rule IDs and rules
    Map<Long, ArrayNode> cpuObjSet = new ConcurrentHashMap<Long, ArrayNode>();
    String nic = null;
    Iterator<FlowRule> it = rules.iterator();
    while (it.hasNext()) {
        NicFlowRule nicRule = (NicFlowRule) it.next();
        if (nicRule.isFullWildcard() && (rulesToInstall > 1)) {
            log.warn("Skipping wildcard flow rule: {}", nicRule);
            it.remove();
            continue;
        }
        long coreIndex = nicRule.cpuCoreIndex();
        // Keep the ID of the target NIC
        if (nic == null) {
            nic = findNicInterfaceWithPort(deviceId, nicRule.interfaceNumber());
            checkArgument(!Strings.isNullOrEmpty(nic), "Attempted to install flow rules in an invalid NIC");
        }
        // Create a JSON array for this CPU core
        if (!cpuObjSet.containsKey(coreIndex)) {
            cpuObjSet.put(coreIndex, mapper.createArrayNode());
        }
        // The array of rules that corresponds to this CPU core
        ArrayNode ruleArrayNode = cpuObjSet.get(coreIndex);
        // Each rule has an ID and a content
        ObjectNode ruleNode = mapper.createObjectNode();
        ruleNode.put(PARAM_ID, nicRule.id().value());
        ruleNode.put(PARAM_RULE_CONTENT, nicRule.ruleBody());
        ruleArrayNode.add(ruleNode);
    }
    if (rules.size() == 0) {
        log.error("Failed to install {} NIC flow rules in device {}", rulesToInstall, deviceId);
        return Collections.EMPTY_LIST;
    }
    ObjectNode nicObjNode = mapper.createObjectNode();
    nicObjNode.put(PARAM_NAME, nic);
    ArrayNode cpusArrayNode = nicObjNode.putArray(PARAM_CPUS);
    // Convert the map of CPU cores to arrays of rules to JSON
    for (Map.Entry<Long, ArrayNode> entry : cpuObjSet.entrySet()) {
        long coreIndex = entry.getKey();
        ArrayNode ruleArrayNode = entry.getValue();
        ObjectNode cpuObjNode = mapper.createObjectNode();
        cpuObjNode.put(PARAM_ID, coreIndex);
        cpuObjNode.putArray(PARAM_RULES).addAll(ruleArrayNode);
        cpusArrayNode.add(cpuObjNode);
    }
    scsObjNode.putArray(PARAM_NICS).add(nicObjNode);
    // Create the object node to host all the data
    ObjectNode sendObjNode = mapper.createObjectNode();
    sendObjNode.putArray(PARAM_RULES).add(scsObjNode);
    // Post the NIC rules to the server
    int response = getController().post(deviceId, URL_RULE_MANAGEMENT, new ByteArrayInputStream(sendObjNode.toString().getBytes()), JSON);
    // Upon an error, return an empty set of rules
    if (!checkStatusCode(response)) {
        log.error("Failed to install {} NIC flow rules in device {}", rules.size(), deviceId);
        return Collections.EMPTY_LIST;
    }
    log.info("Successfully installed {}/{} NIC flow rules in device {}", rules.size(), rulesToInstall, deviceId);
    // .. or all of them
    return rules;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) NicFlowRule(org.onosproject.drivers.server.devices.nic.NicFlowRule) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) NicFlowRule(org.onosproject.drivers.server.devices.nic.NicFlowRule) FlowRule(org.onosproject.net.flow.FlowRule) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 NicFlowRule (org.onosproject.drivers.server.devices.nic.NicFlowRule)2 FlowRule (org.onosproject.net.flow.FlowRule)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Map (java.util.Map)1 Set (java.util.Set)1 DefaultFlowEntry (org.onosproject.net.flow.DefaultFlowEntry)1 FlowEntry (org.onosproject.net.flow.FlowEntry)1