Search in sources :

Example 1 with ExtensionTreatment

use of org.onosproject.net.flow.instructions.ExtensionTreatment in project onos by opennetworkinglab.

the class K8sServiceHandler method setStatefulGroupFlowRules.

private void setStatefulGroupFlowRules(DeviceId deviceId, long ctState, long ctMask, Service service, boolean install) {
    List<GroupBucket> buckets = Lists.newArrayList();
    String serviceName = service.getMetadata().getName();
    String serviceIp = service.getSpec().getClusterIP();
    // TODO: multi-ports case should be addressed
    Integer servicePort = service.getSpec().getPorts().get(0).getPort();
    String serviceProtocol = service.getSpec().getPorts().get(0).getProtocol();
    String svcStr = servicePortStr(serviceIp, servicePort, serviceProtocol);
    int groupId = svcStr.hashCode();
    List<Endpoints> endpointses = k8sEndpointsService.endpointses().stream().filter(ep -> serviceName.equals(ep.getMetadata().getName())).collect(Collectors.toList());
    Map<String, String> nodeIpGatewayIpMap = nodeIpGatewayIpMap(k8sNodeService, k8sNetworkService);
    for (Endpoints endpoints : endpointses) {
        for (EndpointSubset endpointSubset : endpoints.getSubsets()) {
            List<EndpointPort> ports = endpointSubset.getPorts().stream().filter(p -> p.getProtocol().equals(TCP)).collect(Collectors.toList());
            for (EndpointAddress address : endpointSubset.getAddresses()) {
                String podIp = nodeIpGatewayIpMap.containsKey(address.getIp()) ? nodeIpGatewayIpMap.get(address.getIp()) : address.getIp();
                NiciraConnTrackTreatmentBuilder connTreatmentBuilder = niciraConnTrackTreatmentBuilder(driverService, deviceId).commit(true).natAction(true).natIp(IpAddress.valueOf(podIp)).natFlag(CT_NAT_DST_FLAG);
                ports.forEach(p -> {
                    ExtensionTreatment ctNatTreatment = connTreatmentBuilder.natPortMin(TpPort.tpPort(p.getPort())).natPortMax(TpPort.tpPort(p.getPort())).build();
                    ExtensionTreatment resubmitTreatment = buildResubmitExtension(deviceService.getDevice(deviceId), ACL_TABLE);
                    TrafficTreatment treatment = DefaultTrafficTreatment.builder().extension(ctNatTreatment, deviceId).extension(resubmitTreatment, deviceId).build();
                    buckets.add(buildGroupBucket(treatment, SELECT, (short) -1));
                });
            }
        }
    }
    if (!buckets.isEmpty()) {
        k8sGroupRuleService.setRule(appId, deviceId, groupId, SELECT, buckets, install);
        setTrackNew(deviceId, ctState, ctMask, IpAddress.valueOf(serviceIp), TpPort.tpPort(servicePort), NAT_TABLE, groupId, PRIORITY_CT_RULE, install);
    }
}
Also used : DeviceService(org.onosproject.net.device.DeviceService) NODE_IP_PREFIX(org.onosproject.k8snetworking.api.Constants.NODE_IP_PREFIX) RulePopulatorUtil.niciraConnTrackTreatmentBuilder(org.onosproject.k8snetworking.util.RulePopulatorUtil.niciraConnTrackTreatmentBuilder) PRIORITY_CIDR_RULE(org.onosproject.k8snetworking.api.Constants.PRIORITY_CIDR_RULE) NiciraConnTrackTreatmentBuilder(org.onosproject.k8snetworking.util.RulePopulatorUtil.NiciraConnTrackTreatmentBuilder) K8sEndpointsEvent(org.onosproject.k8snetworking.api.K8sEndpointsEvent) DriverService(org.onosproject.net.driver.DriverService) DefaultTrafficTreatment(org.onosproject.net.flow.DefaultTrafficTreatment) StorageService(org.onosproject.store.service.StorageService) ROUTING_TABLE(org.onosproject.k8snetworking.api.Constants.ROUTING_TABLE) Map(java.util.Map) ApplicationId(org.onosproject.core.ApplicationId) RulePopulatorUtil.buildResubmitExtension(org.onosproject.k8snetworking.util.RulePopulatorUtil.buildResubmitExtension) NAT_STATEFUL(org.onosproject.k8snetworking.api.Constants.NAT_STATEFUL) SELECT(org.onosproject.net.group.GroupDescription.Type.SELECT) K8sEndpointsListener(org.onosproject.k8snetworking.api.K8sEndpointsListener) PRIORITY_CT_RULE(org.onosproject.k8snetworking.api.Constants.PRIORITY_CT_RULE) Deactivate(org.osgi.service.component.annotations.Deactivate) Set(java.util.Set) EndpointAddress(io.fabric8.kubernetes.api.model.EndpointAddress) PRIORITY_INTER_ROUTING_RULE(org.onosproject.k8snetworking.api.Constants.PRIORITY_INTER_ROUTING_RULE) SERVICE_IP_NAT_MODE(org.onosproject.k8snetworking.impl.OsgiPropertyConstants.SERVICE_IP_NAT_MODE) TUN_ENTRY_TABLE(org.onosproject.k8snetworking.api.Constants.TUN_ENTRY_TABLE) DeviceId(org.onosproject.net.DeviceId) Dictionary(java.util.Dictionary) Tools(org.onlab.util.Tools) K8sServiceService(org.onosproject.k8snetworking.api.K8sServiceService) ACL_TABLE(org.onosproject.k8snetworking.api.Constants.ACL_TABLE) ComponentContext(org.osgi.service.component.ComponentContext) K8sNetworkingUtil.portNumberByName(org.onosproject.k8snetworking.util.K8sNetworkingUtil.portNumberByName) GROUPING_TABLE(org.onosproject.k8snetworking.api.Constants.GROUPING_TABLE) Component(org.osgi.service.component.annotations.Component) Lists(com.google.common.collect.Lists) TrafficSelector(org.onosproject.net.flow.TrafficSelector) K8sNetworkEvent(org.onosproject.k8snetworking.api.K8sNetworkEvent) K8sNodeEvent(org.onosproject.k8snode.api.K8sNodeEvent) DefaultTrafficSelector(org.onosproject.net.flow.DefaultTrafficSelector) K8S_NETWORKING_APP_ID(org.onosproject.k8snetworking.api.Constants.K8S_NETWORKING_APP_ID) TpPort(org.onlab.packet.TpPort) ComponentConfigService(org.onosproject.cfg.ComponentConfigService) Endpoints(io.fabric8.kubernetes.api.model.Endpoints) EndpointSubset(io.fabric8.kubernetes.api.model.EndpointSubset) Pod(io.fabric8.kubernetes.api.model.Pod) RulePopulatorUtil.computeCtMaskFlag(org.onosproject.k8snetworking.util.RulePopulatorUtil.computeCtMaskFlag) SERVICE_IP_NAT_MODE_DEFAULT(org.onosproject.k8snetworking.impl.OsgiPropertyConstants.SERVICE_IP_NAT_MODE_DEFAULT) IPv4(org.onlab.packet.IPv4) GroupId(org.onosproject.core.GroupId) ExtensionSelector(org.onosproject.net.flow.criteria.ExtensionSelector) B_CLASS(org.onosproject.k8snetworking.api.Constants.B_CLASS) MacAddress(org.onlab.packet.MacAddress) RulePopulatorUtil.buildLoadExtension(org.onosproject.k8snetworking.util.RulePopulatorUtil.buildLoadExtension) K8sNodeService(org.onosproject.k8snode.api.K8sNodeService) LeadershipService(org.onosproject.cluster.LeadershipService) POD_TABLE(org.onosproject.k8snetworking.api.Constants.POD_TABLE) CT_NAT_DST_FLAG(org.onosproject.k8snetworking.util.RulePopulatorUtil.CT_NAT_DST_FLAG) SERVICE_FAKE_MAC_STR(org.onosproject.k8snetworking.api.Constants.SERVICE_FAKE_MAC_STR) CoreService(org.onosproject.core.CoreService) Tools.groupedThreads(org.onlab.util.Tools.groupedThreads) ServicePort(io.fabric8.kubernetes.api.model.ServicePort) K8sNodeListener(org.onosproject.k8snode.api.K8sNodeListener) NAMESPACE_TABLE(org.onosproject.k8snetworking.api.Constants.NAMESPACE_TABLE) K8sNetworkListener(org.onosproject.k8snetworking.api.K8sNetworkListener) NAT_STATELESS(org.onosproject.k8snetworking.api.Constants.NAT_STATELESS) Ethernet(org.onlab.packet.Ethernet) STAT_EGRESS_TABLE(org.onosproject.k8snetworking.api.Constants.STAT_EGRESS_TABLE) A_CLASS(org.onosproject.k8snetworking.api.Constants.A_CLASS) SERVICE_IP_CIDR_DEFAULT(org.onosproject.k8snetworking.impl.OsgiPropertyConstants.SERVICE_IP_CIDR_DEFAULT) K8sNetworkingUtil.podByIp(org.onosproject.k8snetworking.util.K8sNetworkingUtil.podByIp) K8sPodService(org.onosproject.k8snetworking.api.K8sPodService) K8sServiceEvent(org.onosproject.k8snetworking.api.K8sServiceEvent) NodeId(org.onosproject.cluster.NodeId) SERVICE_CIDR(org.onosproject.k8snetworking.impl.OsgiPropertyConstants.SERVICE_CIDR) DST(org.onosproject.k8snetworking.api.Constants.DST) RulePopulatorUtil.computeCtStateFlag(org.onosproject.k8snetworking.util.RulePopulatorUtil.computeCtStateFlag) K8sFlowRuleService(org.onosproject.k8snetworking.api.K8sFlowRuleService) Executors.newSingleThreadExecutor(java.util.concurrent.Executors.newSingleThreadExecutor) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) ClusterService(org.onosproject.cluster.ClusterService) PRIORITY_NAT_RULE(org.onosproject.k8snetworking.api.Constants.PRIORITY_NAT_RULE) IpPrefix(org.onlab.packet.IpPrefix) SRC(org.onosproject.k8snetworking.api.Constants.SRC) GroupBucket(org.onosproject.net.group.GroupBucket) K8sNetworkService(org.onosproject.k8snetworking.api.K8sNetworkService) K8sNetworkingUtil.getBclassIpPrefixFromCidr(org.onosproject.k8snetworking.util.K8sNetworkingUtil.getBclassIpPrefixFromCidr) K8sServiceListener(org.onosproject.k8snetworking.api.K8sServiceListener) Activate(org.osgi.service.component.annotations.Activate) Service(io.fabric8.kubernetes.api.model.Service) K8sNode(org.onosproject.k8snode.api.K8sNode) ExecutorService(java.util.concurrent.ExecutorService) IpAddress(org.onlab.packet.IpAddress) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) Logger(org.slf4j.Logger) RulePopulatorUtil(org.onosproject.k8snetworking.util.RulePopulatorUtil) SHIFTED_IP_PREFIX(org.onosproject.k8snetworking.api.Constants.SHIFTED_IP_PREFIX) EndpointPort(io.fabric8.kubernetes.api.model.EndpointPort) NAT_TABLE(org.onosproject.k8snetworking.api.Constants.NAT_TABLE) ExtensionTreatment(org.onosproject.net.flow.instructions.ExtensionTreatment) K8sGroupRuleService(org.onosproject.k8snetworking.api.K8sGroupRuleService) Maps(com.google.common.collect.Maps) SERVICE_TABLE(org.onosproject.k8snetworking.api.Constants.SERVICE_TABLE) K8sNetworkingUtil.nodeIpGatewayIpMap(org.onosproject.k8snetworking.util.K8sNetworkingUtil.nodeIpGatewayIpMap) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) RulePopulatorUtil.buildGroupBucket(org.onosproject.k8snetworking.util.RulePopulatorUtil.buildGroupBucket) K8sEndpointsService(org.onosproject.k8snetworking.api.K8sEndpointsService) SHIFTED_IP_CIDR(org.onosproject.k8snetworking.api.Constants.SHIFTED_IP_CIDR) K8sNetwork(org.onosproject.k8snetworking.api.K8sNetwork) Modified(org.osgi.service.component.annotations.Modified) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) Reference(org.osgi.service.component.annotations.Reference) EndpointPort(io.fabric8.kubernetes.api.model.EndpointPort) DefaultTrafficTreatment(org.onosproject.net.flow.DefaultTrafficTreatment) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) Endpoints(io.fabric8.kubernetes.api.model.Endpoints) NiciraConnTrackTreatmentBuilder(org.onosproject.k8snetworking.util.RulePopulatorUtil.NiciraConnTrackTreatmentBuilder) EndpointSubset(io.fabric8.kubernetes.api.model.EndpointSubset) GroupBucket(org.onosproject.net.group.GroupBucket) RulePopulatorUtil.buildGroupBucket(org.onosproject.k8snetworking.util.RulePopulatorUtil.buildGroupBucket) ExtensionTreatment(org.onosproject.net.flow.instructions.ExtensionTreatment) EndpointAddress(io.fabric8.kubernetes.api.model.EndpointAddress)

Example 2 with ExtensionTreatment

use of org.onosproject.net.flow.instructions.ExtensionTreatment in project onos by opennetworkinglab.

the class K8sServiceHandler method setSrcDstCidrRules.

private void setSrcDstCidrRules(DeviceId deviceId, String srcCidr, String dstCidr, String cidrClass, String segId, String shiftPrefix, String shiftType, int installTable, int transitTable, int priority, boolean install) {
    TrafficSelector selector = DefaultTrafficSelector.builder().matchEthType(Ethernet.TYPE_IPV4).matchIPSrc(IpPrefix.valueOf(srcCidr)).matchIPDst(IpPrefix.valueOf(dstCidr)).build();
    TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
    if (segId != null) {
        tBuilder.setTunnelId(Long.valueOf(segId));
    }
    if (shiftPrefix != null && shiftType != null) {
        ExtensionTreatment loadTreatment = buildLoadExtension(deviceService.getDevice(deviceId), cidrClass, shiftType, shiftPrefix);
        tBuilder.extension(loadTreatment, deviceId);
    }
    tBuilder.transition(transitTable);
    k8sFlowRuleService.setRule(appId, deviceId, selector, tBuilder.build(), priority, installTable, install);
}
Also used : TrafficSelector(org.onosproject.net.flow.TrafficSelector) DefaultTrafficSelector(org.onosproject.net.flow.DefaultTrafficSelector) DefaultTrafficTreatment(org.onosproject.net.flow.DefaultTrafficTreatment) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) ExtensionTreatment(org.onosproject.net.flow.instructions.ExtensionTreatment)

Example 3 with ExtensionTreatment

use of org.onosproject.net.flow.instructions.ExtensionTreatment in project onos by opennetworkinglab.

the class RulePopulatorUtil method buildExtension.

/**
 * Returns tunnel destination extension treatment object.
 *
 * @param deviceService driver service
 * @param deviceId device id to apply this treatment
 * @param remoteIp tunnel destination ip address
 * @return extension treatment
 */
public static ExtensionTreatment buildExtension(DeviceService deviceService, DeviceId deviceId, Ip4Address remoteIp) {
    Device device = deviceService.getDevice(deviceId);
    if (!checkTreatmentResolver(device)) {
        return null;
    }
    if (device == null) {
        return null;
    }
    ExtensionTreatmentResolver resolver = device.as(ExtensionTreatmentResolver.class);
    ExtensionTreatment treatment = resolver.getExtensionInstruction(NICIRA_SET_TUNNEL_DST.type());
    try {
        treatment.setPropertyValue(TUNNEL_DST, remoteIp);
        return treatment;
    } catch (ExtensionPropertyException e) {
        log.warn("Failed to get tunnelDst extension treatment for {} " + "because of {}", deviceId, e);
        return null;
    }
}
Also used : Device(org.onosproject.net.Device) ExtensionTreatmentResolver(org.onosproject.net.behaviour.ExtensionTreatmentResolver) ExtensionTreatment(org.onosproject.net.flow.instructions.ExtensionTreatment) ExtensionPropertyException(org.onosproject.net.flow.instructions.ExtensionPropertyException)

Example 4 with ExtensionTreatment

use of org.onosproject.net.flow.instructions.ExtensionTreatment in project onos by opennetworkinglab.

the class RulePopulatorUtil method buildLoadExtension.

/**
 * Returns the nicira load extension treatment.
 *
 * @param device        device instance
 * @param cidrClass     CIDR class (a | b)
 * @param ipType        IP type (src|dst)
 * @param shift         shift (e.g., 10.10., 20.20., 10, 20,)
 * @return load extension treatment
 */
public static ExtensionTreatment buildLoadExtension(Device device, String cidrClass, String ipType, String shift) {
    if (!checkTreatmentResolver(device)) {
        return null;
    }
    ExtensionTreatmentResolver resolver = device.as(ExtensionTreatmentResolver.class);
    ExtensionTreatment treatment = resolver.getExtensionInstruction(NICIRA_LOAD.type());
    long dst = 0L;
    if (SRC.equalsIgnoreCase(ipType)) {
        dst = SRC_IP;
    } else if (DST.equals(ipType)) {
        dst = DST_IP;
    }
    long value = calculateUpperBit(cidrClass, shift);
    // we only rewrite the upper x bits with value
    int ofsNbits = 0;
    if (A_CLASS.equals(cidrClass)) {
        ofsNbits = A_CLASS_OFF_SET_BIT << 6 | (REMAINDER_BIT - 1);
    } else if (B_CLASS.equals(cidrClass)) {
        ofsNbits = B_CLASS_OFF_SET_BIT << 6 | (REMAINDER_BIT - 1);
    }
    try {
        treatment.setPropertyValue(OFF_SET_N_BITS, ofsNbits);
        treatment.setPropertyValue(DESTINATION, dst);
        treatment.setPropertyValue(VALUE, value);
        return treatment;
    } catch (ExtensionPropertyException e) {
        log.error("Failed to set nicira load extension treatment for {}", device.id());
        return null;
    }
}
Also used : ExtensionTreatmentResolver(org.onosproject.net.behaviour.ExtensionTreatmentResolver) ExtensionTreatment(org.onosproject.net.flow.instructions.ExtensionTreatment) ExtensionPropertyException(org.onosproject.net.flow.instructions.ExtensionPropertyException)

Example 5 with ExtensionTreatment

use of org.onosproject.net.flow.instructions.ExtensionTreatment in project onos by opennetworkinglab.

the class KubevirtSecurityGroupHandler method initializeAclTable.

private void initializeAclTable(DeviceId deviceId, int recircTable, PortNumber outport, boolean install) {
    ExtensionTreatment ctTreatment = niciraConnTrackTreatmentBuilder(driverService, deviceId).commit(true).build();
    TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
    sBuilder.matchEthType(Ethernet.TYPE_IPV4);
    TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
    tBuilder.extension(ctTreatment, deviceId).setOutput(outport);
    flowRuleService.setRule(appId, deviceId, sBuilder.build(), tBuilder.build(), PRIORITY_ACL_INGRESS_RULE, recircTable, install);
}
Also used : TrafficSelector(org.onosproject.net.flow.TrafficSelector) DefaultTrafficSelector(org.onosproject.net.flow.DefaultTrafficSelector) DefaultTrafficTreatment(org.onosproject.net.flow.DefaultTrafficTreatment) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) ExtensionTreatment(org.onosproject.net.flow.instructions.ExtensionTreatment)

Aggregations

ExtensionTreatment (org.onosproject.net.flow.instructions.ExtensionTreatment)35 DefaultTrafficTreatment (org.onosproject.net.flow.DefaultTrafficTreatment)17 TrafficTreatment (org.onosproject.net.flow.TrafficTreatment)17 DefaultTrafficSelector (org.onosproject.net.flow.DefaultTrafficSelector)16 TrafficSelector (org.onosproject.net.flow.TrafficSelector)16 ExtensionTreatmentResolver (org.onosproject.net.behaviour.ExtensionTreatmentResolver)12 ExtensionPropertyException (org.onosproject.net.flow.instructions.ExtensionPropertyException)11 DeviceId (org.onosproject.net.DeviceId)8 Device (org.onosproject.net.Device)7 MacAddress (org.onlab.packet.MacAddress)3 DriverHandler (org.onosproject.net.driver.DriverHandler)3 Pod (io.fabric8.kubernetes.api.model.Pod)2 IpAddress (org.onlab.packet.IpAddress)2 TpPort (org.onlab.packet.TpPort)2 KubevirtNetworkingUtil.getRouterMacAddress (org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.getRouterMacAddress)2 ExtensionTreatmentType (org.onosproject.net.flow.instructions.ExtensionTreatmentType)2 OFAction (org.projectfloodlight.openflow.protocol.action.OFAction)2 OFActionNiciraCt (org.projectfloodlight.openflow.protocol.action.OFActionNiciraCt)2 OFActionNiciraLoad (org.projectfloodlight.openflow.protocol.action.OFActionNiciraLoad)2 OFActionNiciraMove (org.projectfloodlight.openflow.protocol.action.OFActionNiciraMove)2