/** * Converts byte array of prefix value to IpPrefix object. * * @param value byte array of prefix value * @param length prefix length in bits * @return object of IpPrefix */ public static IpPrefix bytesToPrefix(byte[] value, int length) { if (value.length != IPV4_SIZE) { value = Arrays.copyOf(value, IPV4_SIZE); } IpPrefix ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, length); return ipPrefix; } }
/** * Set the encoded source address. * * @param pfx address prefix */ public void setAddr(IpPrefix pfx) { this.addr = pfx.address(); this.masklen = (byte) pfx.prefixLength(); this.family = (byte) ((this.addr.isIp4()) ? PIM.ADDRESS_FAMILY_IP4 : PIM.ADDRESS_FAMILY_IP6); }
/** * Removes a BGP route for a prefix. The prefix can be either IPv4 or IPv6. * * @param prefix the prefix to use * @return true if the route was found and removed, otherwise false */ boolean removeBgpRoute(IpPrefix prefix) { if (prefix.isIp4()) { return (bgpRibIn4.remove(prefix.getIp4Prefix()) != null); // IPv4 } return (bgpRibIn6.remove(prefix.getIp6Prefix()) != null); // IPv6 }
/** * Check if this IP prefix is a multicast prefix. * * @return true if this prefix a multicast prefix */ public boolean isMulticast() { return isIp4() ? IPV4_MULTICAST_PREFIX.contains(this.getIp4Prefix()) : IPV6_MULTICAST_PREFIX.contains(this.getIp6Prefix()); }
private int getPriorityFromPrefix(IpPrefix prefix) { return (prefix.isIp4()) ? 2000 * prefix.prefixLength() + MIN_IP_PRIORITY : 500 * prefix.prefixLength() + MIN_IP_PRIORITY; }
private void registerDhcpInfo(OpenstackPort openstackPort) { checkNotNull(openstackPort); checkArgument(!openstackPort.fixedIps().isEmpty()); OpenstackSubnet openstackSubnet = openstackService.subnets().stream() .filter(n -> n.networkId().equals(openstackPort.networkId())) .findFirst().orElse(null); if (openstackSubnet == null) { log.warn("Failed to find subnet for {}", openstackPort); return; } Ip4Address ipAddress = openstackPort.fixedIps().values().stream().findFirst().get(); IpPrefix subnetPrefix = IpPrefix.valueOf(openstackSubnet.cidr()); Ip4Address broadcast = Ip4Address.makeMaskedAddress( ipAddress, subnetPrefix.prefixLength()); // TODO: supports multiple DNS servers Ip4Address domainServer = openstackSubnet.dnsNameservers().isEmpty() ? DNS_SERVER_IP : openstackSubnet.dnsNameservers().get(0); IpAssignment ipAssignment = IpAssignment.builder() .ipAddress(ipAddress) .leasePeriod(DHCP_INFINITE_LEASE) .timestamp(new Date()) .subnetMask(Ip4Address.makeMaskPrefix(subnetPrefix.prefixLength())) .broadcast(broadcast) .domainServer(domainServer) .assignmentStatus(Option_RangeNotEnforced) .routerAddress(Ip4Address.valueOf(openstackSubnet.gatewayIp())) .build(); dhcpService.setStaticMapping(openstackPort.macAddress(), ipAssignment); }
if (ipv4Dst.isMulticast()) { if (ipv4Dst.prefixLength() != 32) { log.warn("Multicast specific forwarding objective can only be /32"); fail(fwd, ObjectiveError.BADPARAMS); + " in dev:{}", fwd.id(), fwd.nextId(), deviceId); } else { if (ipv4Dst.prefixLength() > 0) { filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst); } else { filteredSelector.matchEthType(Ethernet.TYPE_IPV4) .matchIPDst(IpPrefix.valueOf("0.0.0.0/1")); complementarySelector.matchEthType(Ethernet.TYPE_IPV4) .matchIPDst(IpPrefix.valueOf("128.0.0.0/1")); defaultRule = true;
srcIpPrefix = IpPrefix.valueOf(srcIpString); if (srcIpPrefix.isIp4()) { selectorBuilder.matchIPSrc(srcIpPrefix); } else { dstIpPrefix = IpPrefix.valueOf(dstIpString); if (dstIpPrefix.isIp4()) { selectorBuilder.matchIPDst(dstIpPrefix); } else { (srcIpPrefix.version() != dstIpPrefix.version())) { if ((srcIpPrefix != null) && srcIpPrefix.isIp6()) { ethType = EthType.IPV6.value(); if ((dstIpPrefix != null) && dstIpPrefix.isIp6()) { ethType = EthType.IPV6.value();
/** * Reads the channel buffer and returns object of IPv4AddressTlv. * * @param cb channelBuffer * @return object of flow spec destination prefix * @throws BgpParseException while parsing BgpFsDestinationPrefix */ public static BgpFsDestinationPrefix read(ChannelBuffer cb) throws BgpParseException { IpPrefix ipPrefix; int length = cb.readByte(); if (length == 0) { byte[] prefix = new byte[] {0}; ipPrefix = Validation.bytesToPrefix(prefix, length); return new BgpFsDestinationPrefix((byte) ipPrefix.prefixLength(), ipPrefix); } int len = length / BYTE_IN_BITS; int reminder = length % BYTE_IN_BITS; if (reminder > 0) { len = len + 1; } if (cb.readableBytes() < len) { Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.MALFORMED_ATTRIBUTE_LIST, cb.readableBytes()); } byte[] prefix = new byte[len]; cb.readBytes(prefix, 0, len); ipPrefix = Validation.bytesToPrefix(prefix, length); return new BgpFsDestinationPrefix((byte) ipPrefix.prefixLength(), ipPrefix); }
ForwardingObjective.Builder buildFlowObjective(DeviceId id, OpenstackSecurityGroupRule sgRule, Ip4Address vmIp, IpPrefix remoteIp) { if (remoteIp != null && remoteIp.equals(IpPrefix.valueOf(vmIp, 32))) { return null; } TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); buildMatchs(sBuilder, sgRule, vmIp, remoteIp); ForwardingObjective.Builder foBuilder = DefaultForwardingObjective.builder() .withSelector(sBuilder.build()) .withTreatment(tBuilder.build()) .withPriority(ACL_RULE_PRIORITY) .withFlag(ForwardingObjective.Flag.SPECIFIC) .fromApp(appId); return foBuilder; }
if (prefix.isIp4()) { selector.matchEthType(Ethernet.TYPE_IPV4); if (prefix.prefixLength() != 0) { selector.matchIPDst(prefix); if (prefix.prefixLength() != 0) { selector.matchIPv6Dst(prefix); prefix.prefixLength() * PRIORITY_MULTIPLIER + PRIORITY_OFFSET; Key key = Key.of(prefix.toString(), appId);
/** * Adds a BGP route. The route can be either IPv4 or IPv6. * * @param bgpRouteEntry the BGP route entry to use */ void addBgpRoute(BgpRouteEntry bgpRouteEntry) { if (bgpRouteEntry.isIp4()) { bgpRoutes4.put(bgpRouteEntry.prefix().getIp4Prefix(), // IPv4 bgpRouteEntry); } else { bgpRoutes6.put(bgpRouteEntry.prefix().getIp6Prefix(), // IPv6 bgpRouteEntry); } }
/** * Tests whether the IP version of this entry is IPv4. * * @return true if the IP version of this entry is IPv4, otherwise false. */ public boolean isIp4() { return ipPrefix.isIp4(); }
private void buildMatchRemoteIp(TrafficSelector.Builder sBuilder, IpPrefix remoteIpPrefix, OpenstackSecurityGroupRule.Direction direction) { if (remoteIpPrefix != null && !remoteIpPrefix.getIp4Prefix().equals(IP_PREFIX_ANY)) { if (direction.equals(OpenstackSecurityGroupRule.Direction.EGRESS)) { sBuilder.matchIPDst(remoteIpPrefix); } else { sBuilder.matchIPSrc(remoteIpPrefix); } } }
/** * Returns the IPv4 address value of the prefix. * * @return the IPv4 address value of the prefix */ public Ip4Address address() { IpAddress a = super.address(); return (Ip4Address) a; }
@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()); } }
if (ipv4Dst.isMulticast()) { if (ipv4Dst.prefixLength() != 32) { log.warn("Multicast specific forwarding objective can only be /32"); fail(fwd, ObjectiveError.BADPARAMS); + " in dev:{}", fwd.id(), fwd.nextId(), deviceId); } else { if (ipv4Dst.prefixLength() > 0) { filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst); } else { filteredSelector.matchEthType(Ethernet.TYPE_IPV4) .matchIPDst(IpPrefix.valueOf("0.0.0.0/1")); complementarySelector.matchEthType(Ethernet.TYPE_IPV4) .matchIPDst(IpPrefix.valueOf("128.0.0.0/1")); defaultRule = true;
/** * Reads the channel buffer and returns object of IPv4AddressTlv. * * @param cb channelBuffer * @return object of flow spec source prefix * @throws BgpParseException while parsing BgpFsSourcePrefix */ public static BgpFsSourcePrefix read(ChannelBuffer cb) throws BgpParseException { IpPrefix ipPrefix; int length = cb.readByte(); if (length == 0) { byte[] prefix = new byte[]{0}; ipPrefix = Validation.bytesToPrefix(prefix, length); return new BgpFsSourcePrefix((byte) ipPrefix.prefixLength(), ipPrefix); } int len = length / BYTE_IN_BITS; int reminder = length % BYTE_IN_BITS; if (reminder > 0) { len = len + 1; } if (cb.readableBytes() < len) { Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.MALFORMED_ATTRIBUTE_LIST, cb.readableBytes()); } byte[] prefix = new byte[len]; cb.readBytes(prefix, 0, len); ipPrefix = Validation.bytesToPrefix(prefix, length); return new BgpFsSourcePrefix((byte) ipPrefix.prefixLength(), ipPrefix); }
/** * Adds a BGP route. The route can be either IPv4 or IPv6. * * @param bgpRouteEntry the BGP route entry to use */ void addBgpRoute(BgpRouteEntry bgpRouteEntry) { if (bgpRouteEntry.isIp4()) { // IPv4 route Ip4Prefix ip4Prefix = bgpRouteEntry.prefix().getIp4Prefix(); bgpRibIn4.put(ip4Prefix, bgpRouteEntry); } else { // IPv6 route Ip6Prefix ip6Prefix = bgpRouteEntry.prefix().getIp6Prefix(); bgpRibIn6.put(ip6Prefix, bgpRouteEntry); } }