builder.fromApp(coreService.registerApplication(appId)); switch (flagStr) { case "SPECIFIC": builder.withFlag(ForwardingObjective.Flag.SPECIFIC); break; case "VERSATILE": builder.withFlag(ForwardingObjective.Flag.VERSATILE); break; case "EGRESS": builder.withFlag(ForwardingObjective.Flag.EGRESS); break; default: if (selectorJson != null) { TrafficSelector trafficSelector = trafficSelectorCodec.decode((ObjectNode) selectorJson, context); builder.withSelector(trafficSelector); if (treatmentJson != null) { TrafficTreatment trafficTreatment = trafficTreatmentCodec.decode((ObjectNode) treatmentJson, context); builder.withTreatment(trafficTreatment); builder.nextStep(nextIdJson.asInt()); forwardingObjective = builder.add(); break; case "REMOVE": forwardingObjective = builder.remove(); break;
.builder().withTreatment(treatment.build()) .withSelector(selector).fromApp(appId).makePermanent() .withFlag(Flag.SPECIFIC).withPriority(MAC_PRIORITY); if (flag) { flowObjectiveService.forward(deviceId, objective.add());
.builder().withTreatment(treatment).withSelector(selector) .fromApp(appId).withFlag(Flag.SPECIFIC) .withPriority(ARP_PRIORITY);
.withSelector(selector) .nextStep(nextObjective.id()) .withPriority(priority) .fromApp(appId) .makePermanent() .withFlag(ForwardingObjective.Flag.SPECIFIC) .add()); devices.add(ingress.deviceId());
.setOutput(tunnelOutPort).build(); ForwardingObjective.Builder objective = DefaultForwardingObjective .builder().withTreatment(builder.build()).withSelector(selector) .fromApp(appId).withFlag(Flag.SPECIFIC) .withPriority(MAC_PRIORITY); if (type.equals(Objective.Operation.ADD)) { flowObjectiveService.forward(deviceId, objective.add());
.builder().withTreatment(treatment.build()) .withSelector(selector).fromApp(appId).makePermanent() .withFlag(Flag.SPECIFIC).withPriority(MAC_PRIORITY); if (type.equals(Objective.Operation.ADD)) { if (Sets.newHashSet(localVmPorts).size() == 0) {
/** * Install a rule for pushing node labels to the device of other nodes. * * @param deviceId device to which flow should be pushed * @param labelId label for the device * @param ipPrefix device for which label is pushed * @param type type of operation * @param bBos is this the end of sync push */ public void advertiseNodeLabelRule(DeviceId deviceId, LabelResourceId labelId, IpPrefix ipPrefix, Objective.Operation type, boolean bBos) { checkNotNull(flowObjectiveService); checkNotNull(appId); TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); selectorBuilder.matchMplsLabel(MplsLabel.mplsLabel(labelId.id().intValue())); selectorBuilder.matchIPSrc(ipPrefix); if (bBos) { selectorBuilder.matchMplsBos(bBos); } TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); ForwardingObjective.Builder forwardingObjective = DefaultForwardingObjective.builder() .withSelector(selectorBuilder.build()).withTreatment(treatment) .withFlag(ForwardingObjective.Flag.VERSATILE).fromApp(appId).makePermanent(); if (type.equals(Objective.Operation.ADD)) { flowObjectiveService.forward(deviceId, forwardingObjective.add()); } else { flowObjectiveService.forward(deviceId, forwardingObjective.remove()); } }
@Override public void programTunnelIn(DeviceId deviceId, SegmentationId segmentationId, Iterable<PortNumber> localTunnelPorts, Objective.Operation type) { if (localTunnelPorts == null) { log.info("No tunnel port in device"); return; } Sets.newHashSet(localTunnelPorts).stream().forEach(tp -> { TrafficSelector selector = DefaultTrafficSelector.builder() .matchInPort(tp).add(Criteria.matchTunnelId(Long .parseLong(segmentationId.toString()))) .build(); TrafficTreatment treatment = DefaultTrafficTreatment.builder() .build(); ForwardingObjective.Builder objective = DefaultForwardingObjective .builder().withTreatment(treatment).withSelector(selector) .fromApp(appId).makePermanent().withFlag(Flag.SPECIFIC) .withPriority(L2_CLASSIFIER_PRIORITY); if (type.equals(Objective.Operation.ADD)) { log.debug("programTunnelIn-->ADD"); flowObjectiveService.forward(deviceId, objective.add()); } else { log.debug("programTunnelIn-->REMOVE"); flowObjectiveService.forward(deviceId, objective.remove()); } }); }
.withSelector(selectorBuilder.build()).withTreatment(treatment) .withFlag(ForwardingObjective.Flag.VERSATILE).fromApp(appId).makePermanent();
@Override public void programRouteRules(DeviceId deviceId, SegmentationId l3Vni, IpAddress dstVmIP, SegmentationId dstVni, MacAddress dstVmGwMac, MacAddress dstVmMac, Operation type) { TrafficSelector selector = DefaultTrafficSelector.builder() .matchEthType(IP_TYPE) .matchTunnelId(Long.parseLong(l3Vni.segmentationId())) .matchIPDst(IpPrefix.valueOf(dstVmIP, PREFIX_LENGTH)).build(); TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); treatment.setEthSrc(dstVmGwMac) .setEthDst(dstVmMac) .add(Instructions.modTunnelId(Long.parseLong(dstVni .segmentationId()))); ForwardingObjective.Builder objective = DefaultForwardingObjective .builder().withTreatment(treatment.build()) .withSelector(selector).fromApp(appId).withFlag(Flag.SPECIFIC) .withPriority(L3FWD_PRIORITY); if (type.equals(Objective.Operation.ADD)) { log.debug("RouteRules-->ADD"); flowObjectiveService.forward(deviceId, objective.add()); } else { log.debug("RouteRules-->REMOVE"); flowObjectiveService.forward(deviceId, objective.remove()); } }
/** * Pushes packet intercept flow rules to the device. * * @param device the device to push the rules to * @param request the packet request */ private void pushRule(Device device, PacketRequest request) { if (!device.type().equals(Device.Type.SWITCH)) { return; } if (!deviceService.isAvailable(device.id())) { return; } ForwardingObjective forwarding = createBuilder(request) .add(new ObjectiveContext() { @Override public void onError(Objective objective, ObjectiveError error) { log.warn("Failed to install packet request {} to {}: {}", request, device.id(), error); } }); objectiveService.forward(device.id(), forwarding); }
/** * Sets the flow rules between traffic from VMs in different Cnode. * * @param vni VNI * @param deviceId device ID * @param hostIp host IP of the VM * @param vmIp fixed IP of the VM */ private void setVxLanFlowRule(String vni, DeviceId deviceId, Ip4Address hostIp, Ip4Address vmIp) { TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); sBuilder.matchEthType(Ethernet.TYPE_IPV4) .matchTunnelId(Long.parseLong(vni)) .matchIPDst(vmIp.toIpPrefix()); tBuilder.extension(buildNiciraExtenstion(deviceId, hostIp), deviceId) .setOutput(getTunnelPort(deviceId)); ForwardingObjective fo = DefaultForwardingObjective.builder() .withSelector(sBuilder.build()) .withTreatment(tBuilder.build()) .withPriority(SWITCHING_RULE_PRIORITY) .withFlag(ForwardingObjective.Flag.SPECIFIC) .fromApp(appId) .add(); flowObjectiveService.forward(deviceId, fo); }
@Override public void programSnatSameSegmentUploadControllerRules(DeviceId deviceId, SegmentationId matchVni, IpAddress srcIP, IpAddress dstIP, IpPrefix prefix, Operation type) { TrafficSelector selector = DefaultTrafficSelector.builder() .matchEthType(Ethernet.TYPE_IPV4) .matchTunnelId(Long.parseLong(matchVni.segmentationId())) .matchIPSrc(IpPrefix.valueOf(srcIP, PREFIC_LENGTH)) .matchIPDst(IpPrefix.valueOf(dstIP, prefix.prefixLength())) .build(); TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); treatment.add(Instructions.createOutput(PortNumber.CONTROLLER)); ForwardingObjective.Builder objective = DefaultForwardingObjective .builder().withTreatment(treatment.build()) .withSelector(selector).fromApp(appId).withFlag(Flag.SPECIFIC) .withPriority(SNAT_SAME_SEG_CON_PRIORITY); if (type.equals(Objective.Operation.ADD)) { flowObjectiveService.forward(deviceId, objective.add()); } else { flowObjectiveService.forward(deviceId, objective.remove()); } }
@Override public void programUserdataClassifierRules(DeviceId deviceId, IpPrefix ipPrefix, IpAddress dstIp, MacAddress dstmac, SegmentationId actionVni, Objective.Operation type) { TrafficSelector selector = DefaultTrafficSelector.builder() .matchEthType(Ethernet.TYPE_IPV4).matchIPSrc(ipPrefix) .matchIPDst(IpPrefix.valueOf(dstIp, 32)).build(); TrafficTreatment treatment = DefaultTrafficTreatment.builder() .setTunnelId(Long.parseLong(actionVni.segmentationId())) .setEthDst(dstmac).build(); ForwardingObjective.Builder objective = DefaultForwardingObjective .builder().withTreatment(treatment).withSelector(selector) .fromApp(appId).withFlag(Flag.SPECIFIC) .withPriority(USERDATA_CLASSIFIER_PRIORITY); if (type.equals(Objective.Operation.ADD)) { log.debug("UserdataClassifierRules-->ADD"); flowObjectiveService.forward(deviceId, objective.add()); } else { log.debug("UserdataClassifierRules-->REMOVE"); flowObjectiveService.forward(deviceId, objective.remove()); } }
private void installLocalLabelRule(DeviceId deviceId, LabelResourceId labelId, PortNumber portNum, TunnelId tunnelId, Boolean isBos, Long labelType, Objective.Operation type) { checkNotNull(flowObjectiveService); checkNotNull(appId); TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); selectorBuilder.matchMplsLabel(MplsLabel.mplsLabel(labelId.id().intValue())); selectorBuilder.matchInPort(portNum); selectorBuilder.matchTunnelId(Long.parseLong(tunnelId.id())); selectorBuilder.matchMplsBos(isBos); selectorBuilder.matchMetadata(labelType); TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); ForwardingObjective.Builder forwardingObjective = DefaultForwardingObjective.builder() .withSelector(selectorBuilder.build()) .withTreatment(treatment) .withFlag(ForwardingObjective.Flag.VERSATILE) .fromApp(appId) .makePermanent(); if (type.equals(Objective.Operation.ADD)) { flowObjectiveService.forward(deviceId, forwardingObjective.add()); } else { flowObjectiveService.forward(deviceId, forwardingObjective.remove()); } } }
/** * Sets the flow rules for traffic between VMs in the same Cnode. * * @param ip4Address VM IP address * @param id device ID to put rules * @param port VM port * @param vni VM VNI */ private void setFlowRuleForVMsInSameCnode(Ip4Address ip4Address, DeviceId id, Port port, String vni) { //For L2 Switching Case TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); sBuilder.matchEthType(Ethernet.TYPE_IPV4) .matchIPDst(ip4Address.toIpPrefix()) .matchTunnelId(Long.parseLong(vni)); tBuilder.setOutput(port.number()); ForwardingObjective fo = DefaultForwardingObjective.builder() .withSelector(sBuilder.build()) .withTreatment(tBuilder.build()) .withPriority(SWITCHING_RULE_PRIORITY) .withFlag(ForwardingObjective.Flag.SPECIFIC) .fromApp(appId) .add(); flowObjectiveService.forward(id, fo); }
/** * Builds a forwarding objective from the given selector, treatment and nextId. * * @param selector selector * @param treatment treatment to apply to packet, can be null * @param nextId next objective to point to for forwarding packet * @param add true to create an add objective, false to create a remove * objective * @return forwarding objective */ private ForwardingObjective buildForwardingObjective(TrafficSelector selector, TrafficTreatment treatment, int nextId, boolean add) { DefaultForwardingObjective.Builder fobBuilder = DefaultForwardingObjective.builder(); fobBuilder.withSelector(selector); if (treatment != null) { fobBuilder.withTreatment(treatment); } if (nextId != -1) { fobBuilder.nextStep(nextId); } fobBuilder.fromApp(appId) .withPriority(ACL_PRIORITY) .withFlag(ForwardingObjective.Flag.VERSATILE); return add ? fobBuilder.add() : fobBuilder.remove(); }
/** * Removes flow rules with the supplied information. * * @param flowObjectiveService flow objective service * @param appId application id * @param deviceId device id to remove this flow rule * @param selector traffic selector * @param flag flag * @param priority priority */ public static void removeRule(FlowObjectiveService flowObjectiveService, ApplicationId appId, DeviceId deviceId, TrafficSelector selector, ForwardingObjective.Flag flag, int priority) { ForwardingObjective fo = DefaultForwardingObjective.builder() .withSelector(selector) .withTreatment(DefaultTrafficTreatment.builder().build()) .withFlag(flag) .withPriority(priority) .fromApp(appId) .remove(); flowObjectiveService.forward(deviceId, fo); } }
@Override public void programSnatSameSegmentRules(DeviceId deviceId, SegmentationId matchVni, IpAddress srcIP, IpAddress dstIP, MacAddress ethDst, MacAddress ethSrc, IpAddress ipSrc, SegmentationId actionVni, Objective.Operation type) { TrafficSelector selector = DefaultTrafficSelector.builder() .matchEthType(Ethernet.TYPE_IPV4) .matchTunnelId(Long.parseLong(matchVni.segmentationId())) .matchIPSrc(IpPrefix.valueOf(srcIP, PREFIC_LENGTH)) .matchIPDst(IpPrefix.valueOf(dstIP, PREFIC_LENGTH)).build(); TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); treatment.setEthDst(ethDst).setEthSrc(ethSrc).setIpSrc(ipSrc) .setTunnelId(Long.parseLong(actionVni.segmentationId())); ForwardingObjective.Builder objective = DefaultForwardingObjective .builder().withTreatment(treatment.build()) .withSelector(selector).fromApp(appId).withFlag(Flag.SPECIFIC) .withPriority(SNAT_SAME_SEG_PRIORITY); if (type.equals(Objective.Operation.ADD)) { flowObjectiveService.forward(deviceId, objective.add()); } else { flowObjectiveService.forward(deviceId, objective.remove()); } }
@Override public void programRules(DeviceId deviceId, IpAddress dstIp, MacAddress ethSrc, IpAddress ipDst, SegmentationId actionVni, Objective.Operation type) { TrafficSelector selector = DefaultTrafficSelector.builder() .matchEthType(Ethernet.TYPE_IPV4) .matchIPDst(IpPrefix.valueOf(dstIp, PREFIX_LENGTH)).build(); TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); treatment.setEthSrc(ethSrc).setIpDst(ipDst) .setTunnelId(Long.parseLong(actionVni.segmentationId())); ForwardingObjective.Builder objective = DefaultForwardingObjective .builder().withTreatment(treatment.build()) .withSelector(selector).fromApp(appId).withFlag(Flag.SPECIFIC) .withPriority(DNAT_PRIORITY); if (type.equals(Objective.Operation.ADD)) { log.debug("RouteRules-->ADD"); flowObjectiveService.forward(deviceId, objective.add()); } else { log.debug("RouteRules-->REMOVE"); flowObjectiveService.forward(deviceId, objective.remove()); } } }