Search in sources :

Example 46 with Configuration

use of org.batfish.datamodel.Configuration in project batfish by batfish.

the class PropertyChecker method checkRoutingLoop.

/*
   * Checks for routing loops in the network. For efficiency reasons,
   * we only check for loops with routers that use static routes since
   * these can override the usual loop-prevention mechanisms.
   */
public AnswerElement checkRoutingLoop(HeaderQuestion q) {
    Graph graph = new Graph(_batfish);
    // Collect all relevant destinations
    List<Prefix> prefixes = new ArrayList<>();
    graph.getStaticRoutes().forEach((router, ifaceName, srs) -> {
        for (StaticRoute sr : srs) {
            prefixes.add(sr.getNetwork());
        }
    });
    SortedSet<IpWildcard> pfxs = new TreeSet<>();
    for (Prefix prefix : prefixes) {
        pfxs.add(new IpWildcard(prefix));
    }
    q.getHeaderSpace().setDstIps(pfxs);
    // Collect all routers that use static routes as a
    // potential node along a loop
    List<String> routers = new ArrayList<>();
    for (Entry<String, Configuration> entry : graph.getConfigurations().entrySet()) {
        String router = entry.getKey();
        Configuration conf = entry.getValue();
        if (conf.getDefaultVrf().getStaticRoutes().size() > 0) {
            routers.add(router);
        }
    }
    Encoder enc = new Encoder(_settings, graph, q);
    enc.computeEncoding();
    Context ctx = enc.getCtx();
    EncoderSlice slice = enc.getMainSlice();
    PropertyAdder pa = new PropertyAdder(slice);
    BoolExpr someLoop = ctx.mkBool(false);
    for (String router : routers) {
        BoolExpr hasLoop = pa.instrumentLoop(router);
        someLoop = ctx.mkOr(someLoop, hasLoop);
    }
    enc.add(someLoop);
    VerificationResult result = enc.verify().getFirst();
    return new SmtOneAnswerElement(result);
}
Also used : Context(com.microsoft.z3.Context) BoolExpr(com.microsoft.z3.BoolExpr) StaticRoute(org.batfish.datamodel.StaticRoute) Configuration(org.batfish.datamodel.Configuration) ArrayList(java.util.ArrayList) SmtOneAnswerElement(org.batfish.symbolic.answers.SmtOneAnswerElement) Prefix(org.batfish.datamodel.Prefix) IpWildcard(org.batfish.datamodel.IpWildcard) Graph(org.batfish.symbolic.Graph) TreeSet(java.util.TreeSet)

Example 47 with Configuration

use of org.batfish.datamodel.Configuration in project batfish by batfish.

the class SynthesizerInputImpl method computeIpsByHostname.

private Map<String, Set<Ip>> computeIpsByHostname() {
    Map<String, Map<String, Interface>> enabledInterfaces = toImmutableMap(_enabledInterfaces, Entry::getKey, enabledInterfacesEntry -> {
        Configuration c = _configurations.get(enabledInterfacesEntry.getKey());
        return toImmutableMap(enabledInterfacesEntry.getValue(), Function.identity(), c.getInterfaces()::get);
    });
    Map<Ip, Set<String>> ipOwners = computeIpOwners(true, enabledInterfaces);
    Map<String, Set<Ip>> map = new HashMap<>();
    /*
     * ipOwners may not contain all nodes (i.e. a node may not own any IPs),
     * so first initialize to make sure there is an entry for each node.
     */
    _enabledInterfaces.keySet().forEach(node -> map.put(node, new HashSet<>()));
    ipOwners.forEach((ip, owners) -> {
        for (String owner : owners) {
            map.get(owner).add(ip);
        }
    });
    // freeze
    return toImmutableMap(map, Entry::getKey, e -> ImmutableSet.copyOf(e.getValue()));
}
Also used : Entry(java.util.Map.Entry) HashSet(java.util.HashSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Configuration(org.batfish.datamodel.Configuration) HashMap(java.util.HashMap) Ip(org.batfish.datamodel.Ip) CommonUtil.toImmutableMap(org.batfish.common.util.CommonUtil.toImmutableMap) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashSet(java.util.HashSet)

Example 48 with Configuration

use of org.batfish.datamodel.Configuration in project batfish by batfish.

the class SynthesizerInputImpl method computeEnabledInterfacesByNodeVrf.

private Map<String, Map<String, Set<String>>> computeEnabledInterfacesByNodeVrf() {
    return toImmutableMap(_enabledVrfs, Entry::getKey, enabledVrfsEntry -> {
        String hostname = enabledVrfsEntry.getKey();
        Set<String> disabledInterfaces = _disabledInterfaces.get(hostname);
        Configuration c = _configurations.get(hostname);
        return toImmutableMap(enabledVrfsEntry.getValue(), Function.identity(), vrfName -> c.getVrfs().get(vrfName).getInterfaces().entrySet().stream().filter(interfaceEntry -> disabledInterfaces == null || !disabledInterfaces.contains(interfaceEntry.getKey())).filter(interfaceEntry -> interfaceEntry.getValue().getActive()).filter(interfaceEntry -> !interfaceEntry.getValue().getBlacklisted()).map(Entry::getKey).collect(ImmutableSet.toImmutableSet()));
    });
}
Also used : HeaderSpace(org.batfish.datamodel.HeaderSpace) ForwardingAnalysis(org.batfish.datamodel.ForwardingAnalysis) CommonUtil.toImmutableMap(org.batfish.common.util.CommonUtil.toImmutableMap) HashMap(java.util.HashMap) BatfishException(org.batfish.common.BatfishException) IpAccessList(org.batfish.datamodel.IpAccessList) Function(java.util.function.Function) Edge(org.batfish.datamodel.Edge) Interface(org.batfish.datamodel.Interface) HashSet(java.util.HashSet) ImmutableList(com.google.common.collect.ImmutableList) Topology(org.batfish.datamodel.Topology) Map(java.util.Map) EmptyIpSpace(org.batfish.datamodel.EmptyIpSpace) Configuration(org.batfish.datamodel.Configuration) LineAction(org.batfish.datamodel.LineAction) HeaderSpaceMatchExpr(org.batfish.z3.expr.HeaderSpaceMatchExpr) Pair(org.batfish.common.Pair) Nullable(javax.annotation.Nullable) BooleanExpr(org.batfish.z3.expr.BooleanExpr) ImmutableSet(com.google.common.collect.ImmutableSet) NetworkFactory(org.batfish.datamodel.NetworkFactory) ImmutableMap(com.google.common.collect.ImmutableMap) IpSpaceMatchExpr(org.batfish.z3.expr.IpSpaceMatchExpr) CommonUtil.computeIpOwners(org.batfish.common.util.CommonUtil.computeIpOwners) Range(com.google.common.collect.Range) Set(java.util.Set) IpSpace(org.batfish.datamodel.IpSpace) AclPermit(org.batfish.z3.state.AclPermit) Maps(com.google.common.collect.Maps) Sets(com.google.common.collect.Sets) List(java.util.List) IpAccessListLine(org.batfish.datamodel.IpAccessListLine) Entry(java.util.Map.Entry) RangeMatchExpr(org.batfish.z3.expr.RangeMatchExpr) Type(org.batfish.z3.state.StateParameter.Type) Ip(org.batfish.datamodel.Ip) Entry(java.util.Map.Entry) Configuration(org.batfish.datamodel.Configuration)

Example 49 with Configuration

use of org.batfish.datamodel.Configuration in project batfish by batfish.

the class IpsecVpnStatusAnswerer method answer.

@Override
public AnswerElement answer() {
    IpsecVpnStatusQuestion question = (IpsecVpnStatusQuestion) _question;
    Map<String, Configuration> configurations = _batfish.loadConfigurations();
    Set<String> includeNodes1 = question.getNode1Regex().getMatchingNodes(configurations);
    Set<String> includeNodes2 = question.getNode2Regex().getMatchingNodes(configurations);
    CommonUtil.initRemoteIpsecVpns(configurations);
    IpsecVpnStatusAnswerElement answerElement = new IpsecVpnStatusAnswerElement();
    for (Configuration c : configurations.values()) {
        if (!includeNodes1.contains(c.getHostname())) {
            continue;
        }
        for (IpsecVpn ipsecVpn : c.getIpsecVpns().values()) {
            IpsecVpnInfo vpnInfo = analyzeIpsecVpn(ipsecVpn);
            if ((vpnInfo.getRemoteEndpoint() == null || includeNodes2.contains(vpnInfo.getRemoteEndpoint().getHostname())) && vpnInfo.getProblems().stream().anyMatch(v -> question.matchesProblem(v))) {
                answerElement.getIpsecVpns().add(vpnInfo);
            }
        }
    }
    return answerElement;
}
Also used : IpsecVpn(org.batfish.datamodel.IpsecVpn) Answerer(org.batfish.common.Answerer) IpsecVpn(org.batfish.datamodel.IpsecVpn) SortedSet(java.util.SortedSet) CommonUtil(org.batfish.common.util.CommonUtil) Set(java.util.Set) TreeSet(java.util.TreeSet) IBatfish(org.batfish.common.plugin.IBatfish) Problem(org.batfish.question.ipsecvpnstatus.IpsecVpnInfo.Problem) Question(org.batfish.datamodel.questions.Question) Map(java.util.Map) Configuration(org.batfish.datamodel.Configuration) AnswerElement(org.batfish.datamodel.answers.AnswerElement) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Configuration(org.batfish.datamodel.Configuration)

Example 50 with Configuration

use of org.batfish.datamodel.Configuration in project batfish by batfish.

the class BdpDataPlanePluginTest method testBgpTieBreaker.

@Test
public void testBgpTieBreaker() {
    String hostname = "r1";
    Configuration c = BatfishTestUtils.createTestConfiguration(hostname, ConfigurationFormat.CISCO_IOS);
    BgpProcess proc = new BgpProcess();
    c.getVrfs().computeIfAbsent(DEFAULT_VRF_NAME, Vrf::new).setBgpProcess(proc);
    Map<String, Node> nodes = new HashMap<String, Node>();
    Node node = new Node(c);
    nodes.put(hostname, node);
    VirtualRouter vr = new VirtualRouter(DEFAULT_VRF_NAME, c);
    // good for both ebgp and ibgp
    BgpMultipathRib bmr = new BgpMultipathRib(vr);
    // ebgp
    BgpBestPathRib ebgpOldBbr = BgpBestPathRib.initial(vr);
    BgpBestPathRib ebgpNewBbr = new BgpBestPathRib(vr, ebgpOldBbr, false);
    BgpRoute.Builder ebgpBuilder = new BgpRoute.Builder().setNetwork(Prefix.ZERO).setOriginType(OriginType.INCOMPLETE).setOriginatorIp(Ip.ZERO).setProtocol(RoutingProtocol.BGP).setReceivedFromIp(Ip.ZERO);
    BgpRoute ebgpOlderHigherOriginator = ebgpBuilder.setOriginatorIp(Ip.MAX).build();
    BgpRoute ebgpNewerHigherOriginator = ebgpBuilder.setOriginatorIp(Ip.MAX).build();
    BgpRoute ebgpLowerOriginator = ebgpBuilder.setOriginatorIp(Ip.ZERO).build();
    // ibgp
    BgpBestPathRib ibgpOldBbr = BgpBestPathRib.initial(vr);
    BgpBestPathRib ibgpNewBbr = new BgpBestPathRib(vr, ibgpOldBbr, false);
    BgpRoute.Builder ibgpBuilder = new BgpRoute.Builder().setNetwork(Prefix.ZERO).setOriginType(OriginType.INCOMPLETE).setOriginatorIp(Ip.ZERO).setProtocol(RoutingProtocol.IBGP).setReceivedFromIp(Ip.ZERO);
    BgpRoute ibgpOlderHigherOriginator = ibgpBuilder.setOriginatorIp(Ip.MAX).build();
    BgpRoute ibgpNewerHigherOriginator = ibgpBuilder.setOriginatorIp(Ip.MAX).build();
    BgpRoute ibgpLowerOriginator = ibgpBuilder.setOriginatorIp(Ip.ZERO).build();
    ebgpOldBbr.mergeRoute(ebgpOlderHigherOriginator);
    ibgpOldBbr.mergeRoute(ibgpOlderHigherOriginator);
    /*
     * Given default tie-breaking, and all more important attributes being equivalent:
     * - When comparing two eBGP adverts, best-path rib prefers older advert.
     * - If neither is older, or one is iBGP, best-path rib prefers advert with higher router-id.
     * - Multipath RIB ignores both age and router-id, seeing both adverts as equal.
     */
    assertThat(ebgpOldBbr.comparePreference(ebgpNewerHigherOriginator, ebgpLowerOriginator), lessThan(0));
    assertThat(ebgpNewBbr.comparePreference(ebgpNewerHigherOriginator, ebgpLowerOriginator), greaterThan(0));
    assertThat(bmr.comparePreference(ebgpNewerHigherOriginator, ebgpLowerOriginator), equalTo(0));
    assertThat(ibgpOldBbr.comparePreference(ibgpNewerHigherOriginator, ibgpLowerOriginator), lessThan(0));
    assertThat(ibgpNewBbr.comparePreference(ibgpNewerHigherOriginator, ibgpLowerOriginator), lessThan(0));
    assertThat(bmr.comparePreference(ibgpNewerHigherOriginator, ibgpLowerOriginator), equalTo(0));
}
Also used : Configuration(org.batfish.datamodel.Configuration) BgpProcess(org.batfish.datamodel.BgpProcess) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Matchers.containsString(org.hamcrest.Matchers.containsString) BgpRoute(org.batfish.datamodel.BgpRoute) Test(org.junit.Test)

Aggregations

Configuration (org.batfish.datamodel.Configuration)170 Test (org.junit.Test)69 Interface (org.batfish.datamodel.Interface)55 Ip (org.batfish.datamodel.Ip)49 Vrf (org.batfish.datamodel.Vrf)45 HashMap (java.util.HashMap)44 Topology (org.batfish.datamodel.Topology)38 VendorConfiguration (org.batfish.vendor.VendorConfiguration)35 Prefix (org.batfish.datamodel.Prefix)33 Edge (org.batfish.datamodel.Edge)32 InterfaceAddress (org.batfish.datamodel.InterfaceAddress)30 Map (java.util.Map)29 Set (java.util.Set)29 TreeMap (java.util.TreeMap)29 BatfishException (org.batfish.common.BatfishException)28 IpAccessList (org.batfish.datamodel.IpAccessList)26 ArrayList (java.util.ArrayList)25 HashSet (java.util.HashSet)25 List (java.util.List)25 SortedSet (java.util.SortedSet)24