diff options
author | Marek Gradzki <mgradzki@cisco.com> | 2017-12-22 15:08:19 +0100 |
---|---|---|
committer | Marek Gradzki <mgradzki@cisco.com> | 2017-12-22 15:22:08 +0100 |
commit | 9a25f286116d8ecd75dcc03a2ef5f7b3d20fc24e (patch) | |
tree | d5e6f47acaba0f9b748763cf7f9c7eb3809787c9 /bgp/bgp-prefix-sid/src | |
parent | 6bb01993fda9b927923ba351d5509ed741ad616e (diff) |
HC2VPP-268: add non-eos MPLS and IP FIB entries
VPP manages separate eos and non-eos forwarding chains,
so non-eos FIB entry is also needed.
Imposing received outgoing label on IP packets
is also required as described in
https://tools.ietf.org/html/draft-ietf-idr-bgp-prefix-sid-07#page-10
and
https://tools.ietf.org/html/draft-ietf-spring-segment-routing-msdc-08#section-4.2.2
Change-Id: Idb91dc44beb64f614f02a95ef0f4b8495c6aeb69
Signed-off-by: Marek Gradzki <mgradzki@cisco.com>
Diffstat (limited to 'bgp/bgp-prefix-sid/src')
4 files changed, 364 insertions, 149 deletions
diff --git a/bgp/bgp-prefix-sid/src/main/java/io/fd/hc2vpp/bgp/prefix/sid/BgpPrefixSidMplsWriter.java b/bgp/bgp-prefix-sid/src/main/java/io/fd/hc2vpp/bgp/prefix/sid/BgpPrefixSidMplsWriter.java index 081a63e9a..8de70bfb3 100644 --- a/bgp/bgp-prefix-sid/src/main/java/io/fd/hc2vpp/bgp/prefix/sid/BgpPrefixSidMplsWriter.java +++ b/bgp/bgp-prefix-sid/src/main/java/io/fd/hc2vpp/bgp/prefix/sid/BgpPrefixSidMplsWriter.java @@ -16,35 +16,21 @@ package io.fd.hc2vpp.bgp.prefix.sid; -import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; -import com.google.common.annotations.VisibleForTesting; import com.sun.istack.internal.Nullable; -import io.fd.hc2vpp.common.translate.util.Ipv4Translator; import io.fd.hc2vpp.common.translate.util.JvppReplyConsumer; import io.fd.honeycomb.translate.bgp.RouteWriter; import io.fd.honeycomb.translate.write.WriteFailedException; import io.fd.vpp.jvpp.core.dto.MplsRouteAddDel; import io.fd.vpp.jvpp.core.future.FutureJVppCore; -import java.util.List; import javax.annotation.Nonnull; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.LabelIndexTlv; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.routes.LabeledUnicastRoutes; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.OriginatorSrgbTlv; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.LabelStack; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.routes.list.LabeledUnicastRoute; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.originator.srgb.tlv.SrgbValue; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.BgpPrefixSid; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.bgp.prefix.sid.BgpPrefixSidTlvs; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.bgp.prefix.sid.bgp.prefix.sid.tlvs.BgpPrefixSidTlv; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.BgpRib; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.Rib; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.LocRib; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.Tables; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.CNextHop; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCase; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -55,13 +41,8 @@ import org.slf4j.LoggerFactory; * @see <a href="https://tools.ietf.org/html/draft-ietf-idr-bgp-prefix-sid-07#section-4.1">Receiving BGP-Prefix-SID * attribute</a> */ -final class BgpPrefixSidMplsWriter implements RouteWriter<LabeledUnicastRoute>, Ipv4Translator, JvppReplyConsumer { - - /** - * Constant used by VPP to disable optional parameters of mpls label type. - */ - @VisibleForTesting - static final int MPLS_LABEL_INVALID = 0x100000; +final class BgpPrefixSidMplsWriter + implements RouteWriter<LabeledUnicastRoute>, MplsRouteRequestProducer, IpRouteRequestProducer, JvppReplyConsumer { private static final Logger LOG = LoggerFactory.getLogger(BgpPrefixSidMplsWriter.class); @@ -81,16 +62,20 @@ final class BgpPrefixSidMplsWriter implements RouteWriter<LabeledUnicastRoute>, public void create(@Nonnull final InstanceIdentifier<LabeledUnicastRoute> id, @Nullable final LabeledUnicastRoute route) throws WriteFailedException.CreateFailedException { - final MplsRouteAddDel request = request(route, true); - LOG.debug("Translating id={}, route={} to {}", id, route, request); - getReplyForCreate(vppApi.mplsRouteAddDel(request).toCompletableFuture(), id, route); + LOG.debug("Translating id={}, route={}", id, route); + // Compute label based on BGP Prefix SID TLVs and add following VPP FIB entries + // (see: https://tools.ietf.org/html/draft-ietf-spring-segment-routing-msdc-08#section-4.2.2): + // + // 1) non-eos VPP MPLS FIB entry (for MPLS packets with derived label in the middle of label stack) + final MplsRouteAddDel mplsRequest = mplsRouteAddDelFor(route, true, LOG); + getReplyForCreate(vppApi.mplsRouteAddDel(mplsRequest).toCompletableFuture(), id, route); + // 2) eos VPP MPLS FIB entry (for MPLS packets with derived label at the end of the label stack) + mplsRequest.mrEos = 1; + getReplyForCreate(vppApi.mplsRouteAddDel(mplsRequest).toCompletableFuture(), id, route); - // TODO(HC2VPP-268): except for SWAP EOS label entry, we should also create: - // 1) SWAP NON-EOS label - // 2) Push label to handle situations when non MPLS packet goes in and its destination is equals to - // the prefix that is being announced (in the example from the draft, it is BGP-Prefix-SID originator loopback): - // https://tools.ietf.org/html/draft-ietf-spring-segment-routing-msdc-06#section-4.2.2 + // 3) VPP IP FIB entry (impose received outbound label on IP packets destined to the BGP prefix) + getReplyForCreate(vppApi.ipAddDelRoute(ipAddDelRouteFor(route, true)).toCompletableFuture(), id, route); LOG.debug("VPP FIB updated successfully (added id={}).", id); } @@ -100,7 +85,17 @@ final class BgpPrefixSidMplsWriter implements RouteWriter<LabeledUnicastRoute>, @Nullable final LabeledUnicastRoute route) throws WriteFailedException.DeleteFailedException { LOG.debug("Removing id={}, route={}", id, route); - getReplyForDelete(vppApi.mplsRouteAddDel(request(route, false)).toCompletableFuture(), id); + // Remove non-eos VPP MPLS FIB entry: + final MplsRouteAddDel mplsRequest = mplsRouteAddDelFor(route, false, LOG); + getReplyForDelete(vppApi.mplsRouteAddDel(mplsRequest).toCompletableFuture(), id); + + // Remove eos VPP MPLS FIB entry: + mplsRequest.mrEos = 1; + getReplyForDelete(vppApi.mplsRouteAddDel(mplsRequest).toCompletableFuture(), id); + + // Remove VPP IP FIB entry: + getReplyForDelete(vppApi.ipAddDelRoute(ipAddDelRouteFor(route, false)).toCompletableFuture(), id); + LOG.debug("VPP FIB updated successfully (removed id={}).", id); } @@ -113,98 +108,6 @@ final class BgpPrefixSidMplsWriter implements RouteWriter<LabeledUnicastRoute>, new UnsupportedOperationException("Operation not supported")); } - private MplsRouteAddDel request(final LabeledUnicastRoute route, boolean isAdd) { - final MplsRouteAddDel request = mplsRouteAddDel(isAdd); - - - translate(route.getAttributes().getCNextHop(), request); - translate(route.getAttributes().getBgpPrefixSid(), request); - translate(route.getLabelStack(), request); - - request.mrEos = 1; - return request; - } - - private MplsRouteAddDel mplsRouteAddDel(final boolean isAdd) { - final MplsRouteAddDel request = new MplsRouteAddDel(); - request.mrIsAdd = booleanToByte(isAdd); - - // default values based on inspecting VPP's CLI and make test code - request.mrClassifyTableIndex = -1; - request.mrNextHopWeight = 1; - request.mrNextHopViaLabel = MPLS_LABEL_INVALID; - return request; - } - - private void translate(@Nonnull final CNextHop cNextHop, @Nonnull final MplsRouteAddDel request) { - checkArgument(cNextHop instanceof Ipv4NextHopCase, - "only ipv4 next hop is supported, but was %s (cNextHop = %s)", cNextHop, cNextHop); - final Ipv4Address nextHop = ((Ipv4NextHopCase) cNextHop).getIpv4NextHop().getGlobal(); - request.mrNextHop = ipv4AddressNoZoneToArray(nextHop.getValue()); - - // We create recursive route. In order to make everything work, - // operator needs to manually map next hop address to proper interface. - // Either via CLI or HC. - // - // VPP can't recursively resolve a route that has out labels via a route that does not have out labels. - // Implicit null label is trick to get around it (no more labels will be added to the package). - // CLI example: - // - // ip route add <next-hop-ip> via <next-hop-ifc> out-labels 3 - request.mrNextHopSwIfIndex = -1; - } - - private void translate(@Nonnull final BgpPrefixSid bgpPrefixSid, @Nonnull final MplsRouteAddDel request) { - Long labelIndex = null; - OriginatorSrgbTlv originatorSrgb = null; - for (BgpPrefixSidTlvs entry : bgpPrefixSid.getBgpPrefixSidTlvs()) { - final BgpPrefixSidTlv tlv = entry.getBgpPrefixSidTlv(); - if (tlv instanceof LabelIndexTlv) { - if (labelIndex != null) { - LOG.warn(" More than one label-index-tlv encountered while parsing bgp-prefix-sid-tlvs: %s." - + "Ignoring all but %s", bgpPrefixSid, labelIndex); - } else { - labelIndex = ((LabelIndexTlv) tlv).getLabelIndexTlv(); - } - } else if (tlv instanceof OriginatorSrgbTlv) { - if (originatorSrgb != null) { - LOG.warn("More than one originator-srgb-tlv encountered while parsing bgp-prefix-sid-tlvs: %s." - + "Ignoring all but %s", bgpPrefixSid, originatorSrgb); - } else { - originatorSrgb = (OriginatorSrgbTlv) tlv; - } - } - } - - // TODO(HC2VPP-272): add support for dynamic (random) label (RFC3107) - - checkArgument(labelIndex != null, "Missing label-index-tlv"); - // TODO(HC2VPP-272): the originator-srgb-tlv is optional, make SRGB range configurable via netconf (requires writeConfig) - checkArgument(originatorSrgb != null, "Missing originator-srgb-tlv"); - // TODO(HC2VPP-272): add support for more than one SRGB - checkArgument(originatorSrgb.getSrgbValue().size() == 1, - "Only one SRGB range is currently supported, but more than one was defined: %s", originatorSrgb); - // Compute local label based on labelIndex value: - final SrgbValue srgbValue = originatorSrgb.getSrgbValue().get(0); - final long srgbStart = srgbValue.getBase().getValue(); - final long localLabel = srgbStart + labelIndex; - final long srgbEnd = srgbStart + srgbValue.getRange().getValue(); - checkArgument(localLabel <= srgbEnd && localLabel >= srgbStart); - request.mrLabel = (int) localLabel; - } - - private void translate(@Nonnull final List<LabelStack> labelStack, @Nonnull final MplsRouteAddDel request) { - final int labelCount = labelStack.size(); - checkArgument(labelCount == 1, "Single label expected, but labelStack.size()==%s", labelCount); - final int label = labelStack.get(0).getLabelValue().getValue().intValue(); - - // TODO(HC2VPP-271): add support for special labels, e.g. implicit null (for PHP). - - // swap one label to another - request.mrNextHopOutLabelStack = new int[] {label}; - request.mrNextHopNOutLabels = 1; - } - // TODO(HC2VPP-268): add test which checks if ID is serializable @Nonnull @Override diff --git a/bgp/bgp-prefix-sid/src/main/java/io/fd/hc2vpp/bgp/prefix/sid/IpRouteRequestProducer.java b/bgp/bgp-prefix-sid/src/main/java/io/fd/hc2vpp/bgp/prefix/sid/IpRouteRequestProducer.java new file mode 100644 index 000000000..4093a040b --- /dev/null +++ b/bgp/bgp-prefix-sid/src/main/java/io/fd/hc2vpp/bgp/prefix/sid/IpRouteRequestProducer.java @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2017 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.hc2vpp.bgp.prefix.sid; + +import static com.google.common.base.Preconditions.checkArgument; +import static io.fd.hc2vpp.bgp.prefix.sid.MplsRouteRequestProducer.MPLS_LABEL_INVALID; + +import io.fd.hc2vpp.common.translate.util.Ipv4Translator; +import io.fd.vpp.jvpp.core.dto.IpAddDelRoute; +import java.util.List; +import javax.annotation.Nonnull; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.LabelStack; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.routes.list.LabeledUnicastRoute; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.CNextHop; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCase; + +interface IpRouteRequestProducer { + /** + * Produces {@link IpAddDelRoute} request that imposes MPLS label received via BGP LU on packets + * destined to the prefix the label was assigned to. + * + * @param route BPG LU route received via BGP + * @param isAdd determines whether to produce request for adding or removing VPP config + * @return jVpp request for updating VPP IP FIB. + * @see <a href="https://tools.ietf.org/html/rfc3107">Carrying Label Information in BGP-4</a> + * @see <a href="https://tools.ietf.org/html/draft-ietf-idr-bgp-prefix-sid-07#page-10">BGP Prefix SID: programming + * outgoing label</a> + * @see <a href="https://tools.ietf.org/html/draft-ietf-spring-segment-routing-msdc-08#section-4.2.2">FIB example + * for SR in the DC usecase</a> + */ + default IpAddDelRoute ipAddDelRouteFor(@Nonnull final LabeledUnicastRoute route, final boolean isAdd) { + final IpAddDelRoute request = Impl.ipAddDelRoute(isAdd); + Impl.translate(route.getPrefix(), request); + Impl.translate(route.getAttributes().getCNextHop(), request); + Impl.translate(route.getLabelStack(), request); + return request; + } + + final class Impl { + private static IpAddDelRoute ipAddDelRoute(final boolean isAdd) { + final IpAddDelRoute request = new IpAddDelRoute(); + request.isAdd = Ipv4Translator.INSTANCE.booleanToByte(isAdd); + + // default values based on inspecting VPP's CLI and make test code + request.classifyTableIndex = -1; + request.nextHopWeight = 1; + request.nextHopViaLabel = MPLS_LABEL_INVALID; + return request; + } + + private static void translate(@Nonnull final IpPrefix prefix, final IpAddDelRoute request) { + // BGP Prefix SID for v6 is not supported + final Ipv4Prefix ipv4Prefix = prefix.getIpv4Prefix(); + checkArgument(ipv4Prefix != null, "Unsupported IpPrefix: %s, ipv4Prefix is missing.", prefix); + request.dstAddressLength = Ipv4Translator.INSTANCE.extractPrefix(ipv4Prefix); + request.dstAddress = Ipv4Translator.INSTANCE.ipv4AddressPrefixToArray(ipv4Prefix); + } + + private static void translate(@Nonnull final CNextHop nextHop, @Nonnull final IpAddDelRoute request) { + checkArgument(nextHop instanceof Ipv4NextHopCase, "only ipv4 next hop is supported, but was %s", nextHop); + + final Ipv4Address nextHopAddress = ((Ipv4NextHopCase) nextHop).getIpv4NextHop().getGlobal(); + request.nextHopAddress = Ipv4Translator.INSTANCE.ipv4AddressNoZoneToArray(nextHopAddress.getValue()); + + // We create recursive route. In order to make everything work, + // operator needs to manually map next hop address to proper interface. + // Either via CLI or HC. + // + // VPP can't recursively resolve a route that has out labels via a route that does not have out labels. + // Implicit null(3) label is trick to get around it (no more labels will be added to the package). + // CLI example: + // + // ip route add <next-hop-prefix> via <next-hop-ifc> out-labels 3 + request.nextHopSwIfIndex = -1; + } + + private static void translate(@Nonnull final List<LabelStack> labelStack, + @Nonnull final IpAddDelRoute request) { + // It is quite possible we could support multiple labels here, but it was never tested + // so it is not supported currently. + final int labelCount = labelStack.size(); + checkArgument(labelCount == 1, "Single label expected, but labelStack.size()==%s", labelCount); + final int label = labelStack.get(0).getLabelValue().getValue().intValue(); + + // TODO(HC2VPP-271): add support for special labels, e.g. implicit null (for PHP). + + // Push label received via BGP on packets destined to the prefix it was assigned to: + request.nextHopOutLabelStack = new int[] {label}; + request.nextHopNOutLabels = 1; + } + } +} diff --git a/bgp/bgp-prefix-sid/src/main/java/io/fd/hc2vpp/bgp/prefix/sid/MplsRouteRequestProducer.java b/bgp/bgp-prefix-sid/src/main/java/io/fd/hc2vpp/bgp/prefix/sid/MplsRouteRequestProducer.java new file mode 100644 index 000000000..659cb991f --- /dev/null +++ b/bgp/bgp-prefix-sid/src/main/java/io/fd/hc2vpp/bgp/prefix/sid/MplsRouteRequestProducer.java @@ -0,0 +1,152 @@ +/* + * Copyright (c) 2017 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.hc2vpp.bgp.prefix.sid; + +import static com.google.common.base.Preconditions.checkArgument; + +import io.fd.hc2vpp.common.translate.util.Ipv4Translator; +import io.fd.vpp.jvpp.core.dto.MplsRouteAddDel; +import java.util.List; +import javax.annotation.Nonnull; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.LabelIndexTlv; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.OriginatorSrgbTlv; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.LabelStack; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.routes.list.LabeledUnicastRoute; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.originator.srgb.tlv.SrgbValue; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.BgpPrefixSid; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.bgp.prefix.sid.BgpPrefixSidTlvs; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.bgp.prefix.sid.bgp.prefix.sid.tlvs.BgpPrefixSidTlv; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.CNextHop; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCase; +import org.slf4j.Logger; + +interface MplsRouteRequestProducer extends Ipv4Translator { + /** + * Constant used by VPP to disable optional parameters of mpls label type. + */ + int MPLS_LABEL_INVALID = 0x100000; + + /** + * Produces {@link MplsRouteAddDel} request for derived local label entry + * that swaps it for the received outbound label. + * + * @param route BPG LU route received via BGP + * @param isAdd determines whether to produce request for adding or removing VPP config + * @return jVpp request for updating VPP MPLS FIB. + * @see <a href="https://tools.ietf.org/html/rfc3107">Carrying Label Information in BGP-4</a> + * @see <a href="https://tools.ietf.org/html/draft-ietf-idr-bgp-prefix-sid-07#page-10">BGP Prefix SID: programming + * outgoing label</a> + * @see <a href="https://tools.ietf.org/html/draft-ietf-spring-segment-routing-msdc-08#section-4.2.2">FIB example + * for SR in the DC usecase</a> + */ + default MplsRouteAddDel mplsRouteAddDelFor(@Nonnull final LabeledUnicastRoute route, final boolean isAdd, + @Nonnull final Logger logger) { + final MplsRouteAddDel request = Impl.mplsRouteAddDel(isAdd); + Impl.translate(route.getAttributes().getCNextHop(), request); + Impl.translate(route.getAttributes().getBgpPrefixSid(), request, logger); + Impl.translate(route.getLabelStack(), request); + return request; + } + + final class Impl implements Ipv4Translator { + private static MplsRouteAddDel mplsRouteAddDel(final boolean isAdd) { + final MplsRouteAddDel request = new MplsRouteAddDel(); + request.mrIsAdd = Ipv4Translator.INSTANCE.booleanToByte(isAdd); + + // default values based on inspecting VPP's CLI and make test code + request.mrClassifyTableIndex = -1; + request.mrNextHopWeight = 1; + request.mrNextHopViaLabel = MPLS_LABEL_INVALID; + return request; + } + + private static void translate(@Nonnull final CNextHop nextHop, @Nonnull final MplsRouteAddDel request) { + checkArgument(nextHop instanceof Ipv4NextHopCase, "only ipv4 next hop is supported, but was %s", nextHop); + final Ipv4Address nextHopAddress = ((Ipv4NextHopCase) nextHop).getIpv4NextHop().getGlobal(); + request.mrNextHop = Ipv4Translator.INSTANCE.ipv4AddressNoZoneToArray(nextHopAddress.getValue()); + + // We create recursive route. In order to make everything work, + // operator needs to manually map next hop address to proper interface. + // Either via CLI or HC. + // + // VPP can't recursively resolve a route that has out labels via a route that does not have out labels. + // Implicit null(3) label is trick to get around it (no more labels will be added to the package). + // CLI example: + // + // ip route add <next-hop-prefix> via <next-hop-ifc> out-labels 3 + request.mrNextHopSwIfIndex = -1; + } + + private static void translate(@Nonnull final BgpPrefixSid bgpPrefixSid, @Nonnull final MplsRouteAddDel request, + @Nonnull final Logger logger) { + Long labelIndex = null; + OriginatorSrgbTlv originatorSrgb = null; + for (BgpPrefixSidTlvs entry : bgpPrefixSid.getBgpPrefixSidTlvs()) { + final BgpPrefixSidTlv tlv = entry.getBgpPrefixSidTlv(); + if (tlv instanceof LabelIndexTlv) { + if (labelIndex != null) { + logger.warn("More than one label-index-tlv encountered while parsing bgp-prefix-sid-tlvs: %s." + + "Ignoring all but %s", bgpPrefixSid, labelIndex); + } else { + labelIndex = ((LabelIndexTlv) tlv).getLabelIndexTlv(); + } + } else if (tlv instanceof OriginatorSrgbTlv) { + if (originatorSrgb != null) { + logger + .warn("More than one originator-srgb-tlv encountered while parsing bgp-prefix-sid-tlvs: %s." + + "Ignoring all but %s", bgpPrefixSid, originatorSrgb); + } else { + originatorSrgb = (OriginatorSrgbTlv) tlv; + } + } + } + + // TODO(HC2VPP-272): add support for dynamic (random) label (RFC3107) + + checkArgument(labelIndex != null, "Missing label-index-tlv"); + // TODO(HC2VPP-272): the originator-srgb-tlv is optional, + // make SRGB range configurable via netconf (requires writeConfig) + checkArgument(originatorSrgb != null, "Missing originator-srgb-tlv"); + // TODO(HC2VPP-272): add support for more than one SRGB + checkArgument(originatorSrgb.getSrgbValue().size() == 1, + "Only one SRGB range is currently supported, but more than one was defined: %s", originatorSrgb); + // Compute local label based on labelIndex value: + final SrgbValue srgbValue = originatorSrgb.getSrgbValue().get(0); + final long srgbStart = srgbValue.getBase().getValue(); + final long localLabel = srgbStart + labelIndex; + final long srgbEnd = srgbStart + srgbValue.getRange().getValue(); + checkArgument(localLabel <= srgbEnd && localLabel >= srgbStart); + request.mrLabel = (int) localLabel; + } + + private static void translate(@Nonnull final List<LabelStack> labelStack, + @Nonnull final MplsRouteAddDel request) { + // It is quite possible we could support multiple labels here, but it was never tested + // so it is not supported currently. + final int labelCount = labelStack.size(); + checkArgument(labelCount == 1, "Single label expected, but labelStack.size()==%s", labelCount); + final int label = labelStack.get(0).getLabelValue().getValue().intValue(); + + // TODO(HC2VPP-271): add support for special labels, e.g. implicit null (for PHP). + + // swap one label to another + request.mrNextHopOutLabelStack = new int[] {label}; + request.mrNextHopNOutLabels = 1; + } + } +} diff --git a/bgp/bgp-prefix-sid/src/test/java/io/fd/hc2vpp/bgp/prefix/sid/BgpPrefixSidMplsWriterTest.java b/bgp/bgp-prefix-sid/src/test/java/io/fd/hc2vpp/bgp/prefix/sid/BgpPrefixSidMplsWriterTest.java index 36df877c1..93b6ac66c 100644 --- a/bgp/bgp-prefix-sid/src/test/java/io/fd/hc2vpp/bgp/prefix/sid/BgpPrefixSidMplsWriterTest.java +++ b/bgp/bgp-prefix-sid/src/test/java/io/fd/hc2vpp/bgp/prefix/sid/BgpPrefixSidMplsWriterTest.java @@ -18,7 +18,11 @@ package io.fd.hc2vpp.bgp.prefix.sid; import static io.fd.hc2vpp.bgp.prefix.sid.BgpPrefixSidMplsWriter.MPLS_LABEL_INVALID; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyZeroInteractions; import static org.mockito.Mockito.when; import static org.mockito.MockitoAnnotations.initMocks; @@ -26,6 +30,8 @@ import com.google.common.collect.Lists; import io.fd.hc2vpp.common.test.util.FutureProducer; import io.fd.hc2vpp.common.translate.util.ByteDataTranslator; import io.fd.honeycomb.translate.write.WriteFailedException; +import io.fd.vpp.jvpp.core.dto.IpAddDelRoute; +import io.fd.vpp.jvpp.core.dto.IpAddDelRouteReply; import io.fd.vpp.jvpp.core.dto.MplsRouteAddDel; import io.fd.vpp.jvpp.core.dto.MplsRouteAddDelReply; import io.fd.vpp.jvpp.core.future.FutureJVppCore; @@ -33,8 +39,10 @@ import java.util.Collections; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.LabeledUnicastRoutes; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.Srgb; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.LabelStackBuilder; @@ -80,15 +88,25 @@ public class BgpPrefixSidMplsWriterTest implements FutureProducer, ByteDataTrans .child(LabeledUnicastRoute.class, new LabeledUnicastRouteKey(pathId, routeKey)); } - private static LabeledUnicastRoute route(final String routeKey, final PathId pathId, - final Ipv4Address nextHopAddress, - final BgpPrefixSid bgpPrefixSid) { + private static LabeledUnicastRoute route(final PathId pathId, final String routeKey) { + final Ipv4Address nextHopAddress = new Ipv4AddressNoZone("5.6.7.8"); + + final BgpPrefixSid bgpPrefixSid = new BgpPrefixSidBuilder() + .setBgpPrefixSidTlvs( + Lists.newArrayList( + labelIndexTlv(102L), + originatorSrgbTlv(16000, 800) + )) + .build(); + final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(new Ipv4NextHopBuilder().setGlobal(nextHopAddress).build()) .build(); + final IpPrefix prefix = new IpPrefix(new Ipv4Prefix("1.2.3.4/24")); return new LabeledUnicastRouteBuilder() .setKey(new LabeledUnicastRouteKey(pathId, routeKey)) .setPathId(pathId) + .setPrefix(prefix) .setAttributes(new AttributesBuilder() .setCNextHop(nextHop) .setBgpPrefixSid(bgpPrefixSid) @@ -106,14 +124,7 @@ public class BgpPrefixSidMplsWriterTest implements FutureProducer, ByteDataTrans .build(); } - @Before - public void setUp() { - initMocks(this); - writer = new BgpPrefixSidMplsWriter(vppApi); - when(vppApi.mplsRouteAddDel(any())).thenReturn(future(new MplsRouteAddDelReply())); - } - - private BgpPrefixSidTlvs originatorSrgbTlv(final long base, final long range) { + private static BgpPrefixSidTlvs originatorSrgbTlv(final long base, final long range) { return new BgpPrefixSidTlvsBuilder() .setBgpPrefixSidTlv(new LuOriginatorSrgbTlvBuilder() .setSrgbValue(Collections.singletonList(new SrgbValueBuilder() @@ -124,27 +135,49 @@ public class BgpPrefixSidMplsWriterTest implements FutureProducer, ByteDataTrans .build(); } + @Before + public void setUp() { + initMocks(this); + writer = new BgpPrefixSidMplsWriter(vppApi); + when(vppApi.mplsRouteAddDel(any())).thenReturn(future(new MplsRouteAddDelReply())); + when(vppApi.ipAddDelRoute(any())).thenReturn(future(new IpAddDelRouteReply())); + } + @Test public void testCreate() throws WriteFailedException.CreateFailedException { final String routeKey = "route-key"; final PathId pathId = new PathId(123L); - final Ipv4Address nextHopAddress = new Ipv4AddressNoZone("5.6.7.8"); + writer.create(id(pathId, routeKey), route(pathId, routeKey)); - final BgpPrefixSid bgpPrefixSid = new BgpPrefixSidBuilder() - .setBgpPrefixSidTlvs( - Lists.newArrayList( - labelIndexTlv(102L), - originatorSrgbTlv(16000, 800) - )) - .build(); - writer.create( - id(pathId, routeKey), - route(routeKey, pathId, nextHopAddress, bgpPrefixSid) - ); - verifyRequest(true); + verify(vppApi, times(2)).mplsRouteAddDel(any()); + // BgpPrefixSidMplsWriter.create reuses DTO for two calls for performance reasons, but mockito + // (InOrder, ArgumentCaptor) works with object references, not values. We are a bit too lazy to use thenAnswer, + // so checking just second invocation: + verify(vppApi, atLeastOnce()).mplsRouteAddDel(getRequest(true, true)); + + verify(vppApi).ipAddDelRoute(getRequest(true)); + } + + @Test + public void testDelete() throws WriteFailedException.DeleteFailedException { + final String routeKey = "route-key"; + final PathId pathId = new PathId(123L); + writer.delete(id(pathId, routeKey), route(pathId, routeKey)); + + verify(vppApi, times(2)).mplsRouteAddDel(any()); + verify(vppApi, atLeastOnce()).mplsRouteAddDel(getRequest(false, true)); + verify(vppApi).ipAddDelRoute(getRequest(false)); + } + + @Test(expected = WriteFailedException.UpdateFailedException.class) + public void testUpdate() throws WriteFailedException.UpdateFailedException { + final String routeKey = "route-key"; + final PathId pathId = new PathId(123L); + writer.update(id(pathId, routeKey), mock(LabeledUnicastRoute.class), mock(LabeledUnicastRoute.class)); + verifyZeroInteractions(vppApi); } - private void verifyRequest(boolean isAdd) { + private MplsRouteAddDel getRequest(boolean isAdd, boolean isEos) { final MplsRouteAddDel request = new MplsRouteAddDel(); request.mrIsAdd = booleanToByte(isAdd); request.mrClassifyTableIndex = -1; @@ -159,7 +192,25 @@ public class BgpPrefixSidMplsWriterTest implements FutureProducer, ByteDataTrans request.mrNextHopOutLabelStack = new int[] {16101}; request.mrNextHopNOutLabels = 1; - request.mrEos = 1; - verify(vppApi).mplsRouteAddDel(request); + request.mrEos = booleanToByte(isEos); + return request; + } + + private IpAddDelRoute getRequest(boolean isAdd) { + final IpAddDelRoute request = new IpAddDelRoute(); + request.isAdd = booleanToByte(isAdd); + request.classifyTableIndex = -1; + request.nextHopWeight = 1; + request.nextHopViaLabel = MPLS_LABEL_INVALID; + + request.dstAddressLength = 24; + request.dstAddress = new byte[] {1, 2, 3, 4}; + + request.nextHopAddress = new byte[] {5, 6, 7, 8}; + request.nextHopSwIfIndex = -1; + + request.nextHopOutLabelStack = new int[] {16101}; + request.nextHopNOutLabels = 1; + return request; } } |